欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

上一篇:

下一篇: