Android-控制输入法及相关用法
程序员文章站
2023-12-23 09:06:16
一. 控制输入法关闭与获取EditView的最大输入长度public class ViewUtils { // 获取编辑框的最大长度,通过反射机制调用隐藏方法 public static int getMaxLength(EditText et) { int length = 0; try { InputFilter[] inputFilters = et.getFilters(); for (InputFi...
一. 控制输入法关闭与获取EditView的最大输入长度
public class ViewUtils {
// 获取编辑框的最大长度,通过反射机制调用隐藏方法
public static int getMaxLength(EditText et) {
int length = 0;
try {
InputFilter[] inputFilters = et.getFilters();
for (InputFilter filter : inputFilters) {
Class<?> c = filter.getClass();
if (c.getName().equals("android.text.InputFilter$LengthFilter")) {
Field[] f = c.getDeclaredFields();
for (Field field : f) {
if (field.getName().equals("mMax")) {
field.setAccessible(true);
length = (Integer) field.get(filter);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return length;
}
public static void hideAllInputMethod(Activity act) {
// 从系统服务中获取输入法管理器
InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) { // 软键盘如果已经打开则关闭之
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}
public static void hideOneInputMethod(Activity act, View v) {
// 从系统服务中获取输入法管理器
InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
// 关闭屏幕上的输入法软键盘
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
二. 在输入框输入固定长度后输入法关闭
/**
* 控制输入框自动关闭输入法
*/
public class HideEditWatcher implements TextWatcher {
private int maxLength;//控件最大长度变量
private EditText editText;//需要监控的控件
private CharSequence sequence;
private Activity activity;
public HideEditWatcher(EditText view, Activity activity) {
editText = view;
this.activity = activity;
maxLength = ViewUtils.getMaxLength(editText);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
sequence = charSequence;
}
@Override
public void afterTextChanged(Editable editable) {
if (sequence == null || sequence.length() == 0) {
return;
}
if (sequence.length() == maxLength) {
ViewUtils.hideOneInputMethod(activity, editText);
}
}
}
<EditText
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="11" />
EditText textView = findViewById(R.id.text);
textView.addTextChangedListener(new HideEditWatcher(textView, this));
三. 当前输入框点击回车自动跳转到下一个输入框
public class JumpTextWatcher implements TextWatcher {
private EditText thisView;
private EditText nextView;
public JumpTextWatcher(EditText thisView, EditText nextView) {
this.thisView = thisView;
this.nextView = nextView;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String str = editable.toString();
if (str.contains("\t") || str.contains("\n")) {
thisView.setText(str.replace("\t", "").replace("\n", ""));
nextView.requestFocus();
nextView.setSelection(nextView.getText().length());
}
}
}
<EditText
android:id="@+id/thisText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/nextText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
EditText thisText = findViewById(R.id.thisText);
EditText nextText = findViewById(R.id.nextText);
thisText.addTextChangedListener(new JumpTextWatcher(thisText, nextText));
本文地址:https://blog.csdn.net/m0_48440239/article/details/110286686
推荐阅读
-
Android-控制输入法及相关用法
-
HTML5移动端开发中的Viewport标签及相关CSS用法解析
-
谈谈PHP中substr和substring的正确用法及相关参数的介绍
-
HTML5移动端开发中的Viewport标签及相关CSS用法解析
-
Java访问控制符原理及具体用法解析
-
讲解Oracle数据库中的数据字典及相关SQL查询用法
-
HTML相关介绍及相关用法
-
Android-控制输入法及相关用法
-
Elasticsearch 控制相关度 (六) - function_score查询中的filter,functions及random_score参数
-
Elasticsearch 控制相关度 (五) - function_score查询及field_value_factor,boost_mode,max_