[Android studio] 第8节 EdiText控件

目录

一、EditText是什么?

二、使用步骤

1.demo



一、EditText是什么?

EditText是Android中的一个文本输入控件,可以用于接收用户的文本输入。下面是EditText控件常用的参数属性详解:

  1. android:id:控件唯一标识符,用于在布局文件和代码中引用该控件。

  2. android:layout_width:控件的宽度,取值可以为具体数值(如20dp)、match_parent(填充父容器的宽度)或wrap_content(根据内容自适应宽度)。

  3. android:layout_height:控件的高度,取值可以为具体数值(如20dp)、match_parent(填充父容器的高度)或wrap_content(根据内容自适应高度)。

  4. android:text:显示在EditText中的文本内容。

  5. android:hint:当EditText为空时显示的提示文本。

  6. android:textColor:EditText中文本的颜色。

  7. android:textSize:EditText中文本的大小。

  8. android:inputType:输入类型,用于限制用户输入的内容。常见的取值有:

    • text:纯文本输入。
    • number:数字输入。
    • phone:电话号码输入。
    • email:电子邮件输入。
    • password:密码输入,字符会被隐藏。
  9. android:maxLines:EditText最大可显示的行数。

  10. android:maxLength:限制用户输入的最大字符数。

  11. android:imeOptions:定义输入法(软键盘)相关的行为选项,比如确定按钮的显示方式。

  12. android:singleLine:是否单行显示文本。

  13. android:enabled:是否启用EditText控件,如果设置为false,则EditText不可编辑。

  14. android:editable:是否可编辑。已废弃,请使用android:enabled代替。

  15. android:gravity:文本对齐方式,可以是topbottomleftrightcenter_verticalcenter_horizontal等值的组合。

  16. android:background:设置EditText的背景,可以是颜色值或者图片资源。

以上是EditText控件常用的参数属性,可以根据需要进行配置,以满足不同的用户输入需求和界面设计要求。

二、使用步骤

1.demo

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DictionaryTableActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="26dp"
        tools:layout_editor_absoluteY="0dp">

        <Button
            android:id="@+id/button_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="button"
            tools:ignore="MissingConstraints" />

        <EditText
            android:id="@+id/edit_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autofillHints="username"
            android:hint="输入内容"
            tools:ignore="MissingConstraints" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
public class DictionaryTableActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dictionary_table);
        Button button = (Button) findViewById(R.id.button_2);
        editText = (EditText) findViewById(R.id.edit_1);
        button.setOnClickListener(this);
    }
    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button_2:
                String inputString =  editText.getText().toString();
                Toast.makeText(DictionaryTableActivity.this, inputString,Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }

}

猜你喜欢

转载自blog.csdn.net/AA2534193348/article/details/131467428
今日推荐