一、效果展示
二、实现步骤
1.设置焦点变更监听器
焦点变更监听器来自于接口
View.OnFocusChangeListener
,若想注册该监听器,就要调用编辑框对象的setOnFocusChangeListener
方法,即可在光标切换之时(获得光标和失去光标)触发焦点变更事 件。
编辑框比较特殊,要点击两次后才会触发点击事件,因为第一次点击只触发焦点变更事件,第二次点击才触发点击事件。编辑框的所谓焦点,直观上就看那个闪动的光标,哪个编辑框有光标,焦点就落在哪里。光标在编辑框之间切换,便产生了焦点变更事件。
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:hint="请输入11位手机号"/>
<EditText
android:id="@+id/pass"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入6位密码"/>
</LinearLayout>
java代码:
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnFocusChangeListener{
private EditText phone;
private EditText password;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phone = findViewById(R.id.phone);
password = findViewById(R.id.pass);
//给编辑框注册一个焦点变更监听器,一旦焦点发生变化,就触发onFocusChange方法
password.setOnFocusChangeListener(this);
}
@Override
//hasFocus代表当前控件是否获得焦点
public void onFocusChange(View v, boolean hasFocus) {
if(v.getId()==R.id.pass&&hasFocus)
{
//获取输入的手机号
String text=phone.getText().toString();
//判断时候为空或者不足11位
if(text.isEmpty()||text.length()<11)
{
//将焦点移到手机号栏中
phone.requestFocus();
Toast.makeText(this,"请输入11位手机号",Toast.LENGTH_LONG).show();
}
}
}
}
2.设置文本变化监听器
输入法的软键盘往往会遮住页面下半部分,使得“登录”“确认”“下一步”等按钮看不到了,用户若想点击这 些按钮还得再点一次返回键才能关闭软键盘。为了方便用户操作,最好在满足特定条件时自动关闭软键 盘,比如手机号码输入满11
位后自动关闭软键盘,又如密码输入满
6
位后自动关闭软键盘,等等。达到指定位数便自动关闭键盘的功能,可以再分解为两个独立的功能点,一个是如何关闭软键盘,另一个是 如何判断已输入的文字达到指定位数。
首先写一个工具类,关闭输入法。
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
public class HideInputMethod {
public static void hideInput(Activity activity, View view)
{
// 从系统服务中获取输入法管理器
InputMethodManager inputMethodManager=(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
// 关闭屏幕上的输入法软键盘
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}
}
然后可以在上个案例的基础上,添加一些java代码即可。
- beforeTextChanged:在文本改变之前触发。
- onTextChanged:在文本改变过程中触发。
- afterTextChanged:在文本改变之后触发。
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.helloworld.util.HideInputMethod;
public class MainActivity extends AppCompatActivity implements View.OnFocusChangeListener{
private EditText phone;
private EditText password;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phone = findViewById(R.id.phone);
password = findViewById(R.id.pass);
//给手机号码编辑框添加文本变化监听器
phone.addTextChangedListener(new HideTextWatcher(phone,11));
//给手机号码编辑框添加文本变化监听器
password.addTextChangedListener(new HideTextWatcher(password,6));
//给编辑框注册一个焦点变更监听器,一旦焦点发生变化,就触发onFocusChange方法
password.setOnFocusChangeListener(this);
}
@Override
//hasFocus代表当前控件是否获得焦点
public void onFocusChange(View v, boolean hasFocus) {
if(v.getId()==R.id.pass&&hasFocus)
{
//获取输入的手机号
String text=phone.getText().toString();
//判断时候为空或者不足11位
if(text.isEmpty()||text.length()<11)
{
//将焦点移到手机号栏中
phone.requestFocus();
Toast.makeText(this,"请输入11位手机号",Toast.LENGTH_LONG).show();
}
}
}
}
//定义一个编辑框监听器,在文本达到指定长度的时候隐藏键盘
private class HideTextWatcher implements TextWatcher
{
private EditText editText;
private int max_length;
//构造函数
public HideTextWatcher(EditText e,int max)
{
super();
editText=e;
max_length=max;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
//我们要使用的是这个方法,在编辑框的输入文本变化后触发
@Override
public void afterTextChanged(Editable s) {
String str=s.toString();//获得已输入的文本字符串
if(s.length()==max_length)
HideInputMethod.hideInput(MainActivity.this,editText);//隐藏输入法
}
}
}