2023年3月9日(软件工程日报)
EditText支持下列XML属性。
inputType:指定输入的文本类型。输入类型的取值说明见表5-4,若同时使用多种文本类型,则可使用竖线“|”把多种文本类型拼接起来。
maxLength:指定文本允许输入的最大长度。
hint:指定提示文本的内容。
textColorHint:指定提示文本的颜色。
text 文本
textPassword 文本密码。显示时用圆点“·”代替
number 整型数
numberSigned 带符号的数字。允许在开头带负号“-”
numberDecimal 带小数点的数字
numberPassword 数字密码。显示时用圆点“·”代替
datetime 时间日期格式。除了数字外,还允许输入横线、斜杆、空格、冒号
date 日期格式。除了数字外,还允许输入横线“-”和斜杆“/”
time 时间格式。除了数字外,还允许输入冒号“:”
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="下面是登录信息"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLength="10"
android:hint="请输入用户名"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:maxLength="8"
android:hint="请输入密码"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
焦点变更监听器
常与EditText连用,用于规范输入
EditText et_password = findViewById(R.id.et_password);
et_password.setOnFocusChangeListener(this);
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (v.getId()==R.id.et_password && hasFocus) {
String phone = et_phone.getText().toString();
if (TextUtils.isEmpty(phone) || phone.length()<11) {
et_phone.requestFocus();
Toast.makeText(this, "请输入11位手机号码", Toast.LENGTH_SHORT).show();
}
}
}