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();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南