📜  Android中带有示例的Java的EditText小部件

📅  最后修改于: 2021-05-09 02:59:36             🧑  作者: Mango

窗口小部件是指UI(用户界面)的元素,可帮助用户与Android App进行交互。 Edittext是许多此类小部件之一,可用于从用户检索文本数据。

Edittext是指显示一个空文本字段的小部件,用户可以在其中输入所需的文本,并且此文本将在我们的应用程序中进一步使用。

类语法:

public class EditText
extends TextView

类层次结构:

java.lang.Object
  ↳android.view.View
     ↳ android.widget.TextView
          ↳ android.widget.EditText

句法:


    .
    .
    
    .
    .

这里的布局可以是任何布局,例如相对,线性等(请参阅本文以了解有关布局的更多信息)。在本文下面给出的表中,属性可能很多。

例子:

>

    

    

如何在Android应用中包含Edittext:

  1. 首先,创建一个新的Android应用程序,或使用一个现有的应用程序对其进行编辑。在两种情况下,都必须有一个XML布局活动文件和一个与此活动链接的Java类文件。
  2. 打开“活动”文件,并在活动的布局(activity_main.xml)文件中包含一个Edittext字段,并在activity_main.xml文件中也添加一个Button。
  3. 现在在Java文件中,将此布局文件与以下代码链接:
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    

    其中activity_main是要附加的布局文件的名称。

  4. 现在,我们将在MainActivity中添加代码。 Java文件,以使我们的布局具有交互性或响应性。我们的应用程序将在单击带有文本“ Welcome to GeeksforGeeks [用户输入的名称] ”的按钮时产生敬酒。
  5. 下面给出了布局文件和Java文件的完整代码。

下面是上述方法的实现:

activity_main.xml

  

  
    
  
    


MainActivity.java
package com.project.edittext;
  
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    private EditText editText;
    private Button button;
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        editText
            = (EditText)findViewById(R.id.edittext_id);
        button
            = (Button)findViewById(R.id.button_id);
  
        button.setOnClickListener(
            new View.OnClickListener() {
  
                @Override
                public void onClick(View v)
                {
                    String name
                        = editText.getText()
                              .toString();
                    Toast.makeText(MainActivity.this,
                                   "Welcome to GeeksforGeeks "
                                       + name,
                                   Toast.LENGTH_SHORT)
                        .show();
                }
            });
    }
}


输出:启动应用程序并在EditText中输入名称。然后将显示EditText中的名称:

Android中Edittext的XML属性

Attributes Description
android:id Used to uniquely identify the control
android:gravity Used to specify how to align the text like left, right, center, top, etc.
android:hint Used to display the hint text when text is empty
android:text Used to set the text of the EditText
android:textSize Used to set size of the text.
android:textColor Used to set color of the text.
android:textStyle Used to set style of the text. For example, bold, italic, bolditalic etc.
android:textAllCaps Used this attribute to show the text in capital letters.
android:width It makes the TextView be exactly this many pixels wide.
android:height It makes the TextView be exactly this many pixels tall.
android:maxWidth Used to make the TextView be at most this many pixels wide.
android:minWidth Used to make the TextView be at least this many pixels wide.
android:background Used to set background to this View.
android:backgroundTint Used to set tint to the background of this view.
android:clickable Used to set true when you want to make this View clickable. Otherwise, set false.
android:drawableBottom Used to set drawable to bottom of the text in this view.
android:drawableEnd Used to set drawable to end of the text in this view.
android:drawableLeft Used to set drawable to left of the text in this view.
android:drawablePadding Used to set padding to drawable of the view.
android:drawableRight Used to set drawable to right of the text in this view.
android:drawableStart Used to set drawable to start of the text in this view.
android:drawableTop Used to set drawable to top of the text in this view.
android:elevation Used to set elevation to this view.