| | |
| | | package com.dayu.qihealonelibrary.activity; |
| | | |
| | | import android.os.Bundle; |
| | | import android.text.Editable; |
| | | import android.text.InputFilter; |
| | | import android.text.Spanned; |
| | | import android.text.TextUtils; |
| | | import android.text.TextWatcher; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | | |
| | |
| | | } |
| | | } |
| | | }); |
| | | // 设置输入过滤器 |
| | | binding.villageNewNum.addTextChangedListener(new TextWatcher() { |
| | | @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) { |
| | | if (!isValidInput(s.toString())) { |
| | | // 如果输入的字符不符合要求,则将 EditText 的内容设置为最后一次合法的输入 |
| | | binding.villageNewNum.setText(s.subSequence(0, s.length() - 1).toString().toUpperCase()); |
| | | binding.villageNewNum.setSelection(binding.villageNewNum.getText().length()); // 将光标移动到末尾 |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | } |
| | | // 检查输入的字符是否符合要求 |
| | | private boolean isValidInput(String input) { |
| | | return input.matches("[0-9a-fA-F]*"); // 包含 0-9 和 a-f 的字符 |
| | | } |
| | | } |