运行有问题或需要源码请点赞关注收藏后评论区留言~~~
一、编辑框EditText
编辑框EditText用于接收软键盘输入的文字,例如用户名,密码,评价内容等等,它由文本视图派生而来。 属性和方法如下图
接下来通过XML布局观看编辑框界面效果
当输满指定的位数之后就无法输入,并且选中时可以下划线高亮 比较美观
EditSimpleActivity类代码如下
package com.example.chapter05;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class EditSimpleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_simple);
}
}
activity_edit_shapeXML文件代码如下
<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" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="下面是手机信息"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="11"
android:hint="请输入11位手机号码"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:maxLength="6"
android:hint="请输入6位服务密码"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
同样为了美观我们可以把输入框调节成圆角矩形 效果如下
部分代码如下
package com.example.chapter05;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class EditBorderActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_border);
}
}
XML文件如下
<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" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="这是默认边框"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:inputType="text"
android:hint="我的边框不见了"
android:background="@null"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:inputType="text"
android:hint="我的边框是圆角"
android:background="@drawable/editext_selector"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
二、焦点变更监听器
虽然编辑框提供了输入文本的最大长度,但是它没提供最小长度,所以这个时候需要焦点变更监听器来检测,点击时会触发焦点变更事件,可以在光标切换时,触发焦点变更事件。 效果如下
当输入的用户名不满足指定位数并且点击密码框时,下方会提示请输入手机号码 这样可以启动自动检测输入是否合法的效果
EditFocusActivity类代码如下
package com.example.chapter05;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class EditFocusActivity extends AppCompatActivity implements View.OnClickListener, View.OnFocusChangeListener {
private EditText et_phone; // 声明一个编辑框对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_focus);
// 从布局文件中获取名叫et_phone的手机号码编辑框
et_phone = findViewById(R.id.et_phone);
// 从布局文件中获取名叫et_password的密码编辑框
EditText et_password = findViewById(R.id.et_password);
// 给密码编辑框注册点击事件监听器
et_password.setOnClickListener(this);
// 给密码编辑框注册一个焦点变化监听器,一旦焦点发生变化,就触发监听器的onFocusChange方法
et_password.setOnFocusChangeListener(this);
}
// 焦点变更事件的处理方法,hasFocus表示当前控件是否获得焦点。
// 为什么光标进入事件不选onClick?因为要点两下才会触发onClick动作(第一下是切换焦点动作)
@Override
public void onFocusChange(View v, boolean hasFocus) {
// 判断密码编辑框是否获得焦点。hasFocus为true表示获得焦点,为false表示失去焦点
if (v.getId()==R.id.et_password && hasFocus) {
String phone = et_phone.getText().toString();
if (TextUtils.isEmpty(phone) || phone.length()<11) { // 手机号码不足11位
// 手机号码编辑框请求焦点,也就是把光标移回手机号码编辑框
et_phone.requestFocus();
Toast.makeText(this, "请输入11位手机号码", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onClick(View v) {
// 编辑框比较特殊,要点击两次后才会触发点击事件,因为第一次点击只触发焦点变更事件,第二次点击才触发点击事件
if (v.getId() == R.id.et_password) {
String phone = et_phone.getText().toString();
if (TextUtils.isEmpty(phone) || phone.length()<11) { // 手机号码不足11位
// 手机号码编辑框请求焦点,也就是把光标移回手机号码编辑框
et_phone.requestFocus();
Toast.makeText(this, "请输入11位手机号码", Toast.LENGTH_SHORT).show();
}
}
}
}
activity_edit_focusXML文件代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="请输入11位手机号码"
android:inputType="number"
android:maxLength="11"
android:background="@drawable/editext_selector"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:hint="请输入6位密码"
android:inputType="numberPassword"
android:maxLength="6"
android:background="@drawable/editext_selector"
android:textColor="@color/black"
android:textSize="17sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
三、文本变化监听器
输入法的软键盘往往后会遮住页面下半部分,使得登录,确认,下一步等按钮看不到了,用户若想点击这些按钮还得再点一次返回键才能关闭软键盘,为了方便用户操作,最好在满足特定条件时自动关闭软键盘,比如手机号码输入满11位后自动关闭软键盘。
输入法软键盘由系统服务INPUT_METHOD_SERVICE管理,所以关闭软键盘也要由该服务处理。
该功能点要求实时监控当前已输入的文本长度,这个监控操作用到文本监听器接口TextWatcher,该接口提供了三个监控方法 具体说明如下
1:beforeTextChanged 文本改变之前触发
2:onTextChanged 文本改变过程中触发
3:afterTextChanged 文本改变之后触发
下面通过一个实例,当输入11位的手机号码和6位的密码后自动关闭收起键盘
EditHideActivity类代码如下
package com.example.chapter05;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import com.example.chapter05.util.ViewUtil;
public class EditHideActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_hide);
// 从布局文件中获取名叫et_phone的手机号码编辑框
EditText et_phone = findViewById(R.id.et_phone);
// 从布局文件中获取名叫et_password的密码编辑框
EditText et_password = findViewById(R.id.et_password);
// 给手机号码编辑框添加文本变化监听器
et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 11));
// 给密码编辑框添加文本变化监听器
et_password.addTextChangedListener(new HideTextWatcher(et_password, 6));
}
// 定义一个编辑框监听器,在输入文本达到指定长度时自动隐藏输入法
private class HideTextWatcher implements TextWatcher {
private EditText mView; // 声明一个编辑框对象
private int mMaxLength; // 声明一个最大长度变量
public HideTextWatcher(EditText v, int maxLength) {
super();
mView = v;
mMaxLength = maxLength;
}
// 在编辑框的输入文本变化前触发
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 s) {
String str = s.toString(); // 获得已输入的文本字符串
// 输入文本达到11位(如手机号码),或者达到6位(如登录密码)时关闭输入法
if ((str.length() == 11 && mMaxLength == 11)
|| (str.length() == 6 && mMaxLength == 6)) {
ViewUtil.hideOneInputMethod(EditHideActivity.this, mView); // 隐藏输入法软键盘
}
}
}
}
activity_edit_hideXML文件代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="输入11位时自动隐藏输入法"
android:inputType="number"
android:maxLength="11"
android:background="@drawable/editext_selector"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:hint="输入6位时自动隐藏输入法"
android:inputType="numberPassword"
android:maxLength="6"
android:background="@drawable/editext_selector"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
创作不易 觉得有帮助请点赞关注收藏