Android编辑框EditText与焦点变更监视器及文本变化监视器实现流程详解
作者:Shewyoo 发布时间:2021-06-21 04:48:14
一、编辑框EditText
编辑框用于接收键盘输入的文字,由文本视图派生而来,除了TextView已有的各种属性和方法,EditText还支持下列XML属性:
inputType:指定输入的文本类型,输入类型的取值说明如下表,若同时使用多种文本类型,则可使用竖线“|”把多种文本类型拼接起来。
maxLength:指定文本允许输入的最大长度。
hint:指定提示文本的内容。
textColorHint:指定提示文本的颜色。
输入类型 | 说明 |
text | 文本 |
textPassword | 文本密码,显示时用圆点代替 |
number | 整型数 |
numberSigned | 带符号的数字,允许在开头带符号”-“ |
numberPassword | 数字密码,显示时用圆点代替 |
datetime | 时间日期格式,除了数字外,还允许输入横线、斜杠、空格、冒号 |
date | 日期格式,除了数字外,还允许输入横线”-“和斜杠”/“ |
time | 时间格式,除了数字外,还允许输入冒号”:“ |
numberDecimal | 带小数点的数字 |
例1:输入用户名和密码
<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="numberPassword"
android:maxLength="10"
android:hint="请输入密码"
android:textColor="@color/black"
android:textSize="17sp"/>
例2:自定义输入框样式:无边框和圆角边框
drawable下新建两个xml文件用于配置样式
第一个:选中时的样式
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--指定形状内的填充颜色-->
<solid android:color="#ffffff"/>
<!-- 指定形状轮廓的粗细与颜色-->
<stroke
android:width="1dp"
android:color="#0000ff"/>
<!-- 指定形状四个圆角的半径-->
<corners android:radius="5dp"/>
<!-- 指定形状四个方向的间距-->
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp"/>
</shape>
第二个:正常样式
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--指定形状内的填充颜色-->
<solid android:color="#ffffff"/>
<!-- 指定形状轮廓的粗细与颜色-->
<stroke
android:width="1dp"
android:color="#aaaaaa"/>
<!-- 指定形状四个圆角的半径-->
<corners android:radius="5dp"/>
<!-- 指定形状四个方向的间距-->
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp"/>
</shape>
再在drawable文件下新建一个xml文件用于配置选中时和未选中时的样式。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/edit_focus"
android:state_focused="true"/>
<item android:drawable="@drawable/edit_normal"/>
</selector>
Activity的XML文件
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:layout_marginTop="10dp"
android:hint="无边框"
android:inputType="text"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit_text"
android:layout_marginTop="10dp"
android:hint="圆角边框"
android:inputType="text"/>
二、焦点变更监视器
调用编辑框对象的setOnFocusChangeListener方法,即可在光标切换时触发焦点变更事件。
使用场景如:手机号码未输满11位,就点击密码框,此时校验不通过,一边弹出提示文字,一边把焦点拉回手机框。
例:当手机号码不足11位时点击密码框会出现提示。
注意:不可采取这样的方式:为密码框绑定点击事件,当点击密码框时检测是否通过。
原因:编辑框点击两次后才会触发点击事件,第一次点击只触发焦点变更事件,第二次点击才触发点击事件。
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit_text"
android:layout_marginTop="10dp"
android:hint="请输入11位手机号码"
android:inputType="number"
android:maxLength="11"/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit_text"
android:layout_marginTop="10dp"
android:hint="请输入密码"
android:inputType="numberPassword"
android:maxLength="11"/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"/>
</LinearLayout>
java类
public class EditFocusActivity extends AppCompatActivity implements View.OnFocusChangeListener {
private EditText et_phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_focus);
et_phone = findViewById(R.id.et_phone);
EditText et_password = findViewById(R.id.et_password);
et_password.setOnFocusChangeListener(this);
}
@Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
String phone = et_phone.getText().toString();
//如果手机号码不足11位或为空
if(TextUtils.isEmpty(phone)||phone.length()<11){
//手机号码编辑框请求焦点,把光标移回手机号码编辑框
et_phone.requestFocus();
Toast.makeText(this,"请输入11位的!",Toast.LENGTH_SHORT).show();
}
}
}
}
三、文本变化 *
调用编辑框对象的addTextChangedListener方法即可注册文本 * 。
文本 * 的接口名称为TextWatcher,该接口提供了3个监控方法,具体说明:
beforeTextChanged:在文本改变之前触发。
onTextChanged:在文本改变过程种触发。
afterTextChanged:在文本改变后触发。
mEtPassword.addTextChangedListener(new MyWatcher());
class MyWatcher implements TextWatcher {
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
public void onTextChanged(CharSequence s, int start, int before,int count) {
}
public void afterTextChanged(Editable edit) {
}
}
来源:https://blog.csdn.net/Tir_zhang/article/details/126980126
猜你喜欢
- 前言MyBatis中也提供了注解式开发⽅式,采⽤注解可以减少Sql映射⽂件的配置。 当然,使⽤注解式开发的话,sql语句是写在java程序中
- 这是一篇入门级文章,高手请略过。在这篇文章中我们将学习如何用 Java 对图像进行剪裁并将剪裁出来的部分单独保存到文件中。我们将通过以下步骤
- 实现多表联合查询还是在david.mybatis.model包下面新建一个Website类,用来持久化数据之用,重写下相应toString(
- 本文实例为大家分享了java使用HashMap实现斗地主的具体代码,供大家参考,具体内容如下案例介绍按照斗地主的规则,完成洗牌发牌的动作。
- 员工管理系统1、准备工作资料下载内含源码 + 笔记 + web素材源码下载地址:http://xiazai.jb51.net/202105/
- 网上的文章基本上都是只有多数据源或只有动态数据源,而最近的项目需要同时使用两种方式,记录一下配置方法供大家参考。应用场景项目需要同时连接两个
- 1 什么是cookie浏览器与WEB服务器之间是使用HTTP协议进行通信的,当某个用户发出页面请求时,WEB服务器只是简单的进行响应,然后就
- 本文实例讲述了Android编程实现全局获取Context及使用Intent传递对象的方法。分享给大家供大家参考,具体如下:一、全局获取 C
- 一、结构型模式结构型模式有什么好处?从程序的结构上实现松耦合,从而可以扩大整体的类结构,用来解决更大的问题二、适配器模式USB网线转换器三、
- DataSource在数据库应用中,客户端与数据库服务端建立的连接对象(Connection)是宝贵的资源,每次请求数据库都创建连接,使用完
- 本文实例为大家分享了C#强制转换和尝试转换的方法,供大家参考,具体内容如下将String[]类型的Object类型,转换为String[]类
- Java中的Static class详解Java中的类可以是static吗?答案是可以。在Java中我们可以有静态实例变量、静态
- 本文实例讲述了Java解析Excel内容的方法。分享给大家供大家参考。具体实现方法如下:import java.io.File;
- Failed to fectch URl
- 什么是耦合性耦合性(Coupling),也叫耦合度,是对模块间关联程度的度量。模块间的耦合度是指模块之间的依赖关系,包括控制关系、调用关系、
- 一.服务器端获取Session对象依赖于客户端携带的Cookie中的JSESSIONID数据。如果用户把浏览器的隐私级别调到最高,这时浏览器
- 由于一些不可控因素的影响,比如系统内存,计算机状态等,每一次在while循环中执行的次数会有一定差异大概几百次。这就导致了结果的差异。注意这
- 1. 背景我们都知道,Compose可以使用mutableStateOf和UI进行绑定,改变值之后,就可以改变UI。var value by
- Java goto语句妙用今天和朋友聊天的时候,无意间聊到了 goto 语句,但是在 Java 中, goto 是保留关键字,但是朋友说 J
- 看效果由于web 经验弱爆- - 一开始我的思路是找事件,但是看了半天API 基本都是点击触摸,通过物理触发- -我居然忽略了生