方法:java中使用addTextChangedListener,添加 TextWatcher。不可行!!! (具体见最下面注释)
Java代码:
public class MainActivity extends Activity { private EditText et3, et4; private final static String ET3_DIGITS = "abcd"; private final static String ET4_DIGITS = "wxyz"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et3 = (EditText) findViewById(R.id.et3); et4 = (EditText) findViewById(R.id.et4); //方式3:java中使用setKeyListener,添加DigitsKeyListener。 et3.setKeyListener(DigitsKeyListener.getInstance(ET3_DIGITS)); //方法4:java中使用setFilters,添加InputFilter。 et4.setFilters(new InputFilter[] {new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { //source:你即将输入的字符序列 Log.d("jie","source = "+source); //start:默认为0, end:你即将输入的字符序列的长度 Log.d("jie","start = "+ start); Log.d("jie","end = "+ end); //dest:当前EditText显示的内容 Log.d("jie","dest = "+dest); //经测试dstart和dend 总是相等,都表示输入前光标所在位置 Log.d("jie","dstart = "+dstart); Log.d("jie","dend = "+dend); StringBuffer sb = new StringBuffer(); for (int i = 0; i < source.length(); i++) { if (ET4_DIGITS.indexOf(source.charAt(i)) >= 0) { sb.append(source.charAt(i)); } } return sb; } }}); //使用TextWatcher,虽然能够检测到text 内容修改,但是不能进行重新赋值,因为你如果重新setText,又会重新触发该监听,会形成死循环,最终导致栈溢出。故该方法不可行 /*et3.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub StringBuffer sb = new StringBuffer(); for (int i = 0; i < s.length(); i++) { if (ET3_DIGITS.indexOf(s.charAt(i)) >= 0) { sb.append(s.charAt(i)); } } et3.setText(sb); } });*/ } }
xml中控制:
<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" tools:context=".MainActivity" android:orientation="vertical" > <!--方法1 xml中配置inputType。--> <EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="只限数字(xml中 配置inputType)" android:inputType="number" /> <!--方法2 xml中配置digits --> <EditText android:id="@+id/et2" android:layout_width="match_parent" android:layout_height="wrap_content" android:digits=" .@~-,:*?!#/\\=+?^;%$()[]{}|`<>&"'_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLIMNOPQRSTUVWXYZ" android:hint="只限自定义特殊字符(xml中 配置digits)" /> <EditText android:id="@+id/et3" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="只限abcd(java中使用addTextChangedListener)" /> <EditText android:id="@+id/et4" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="只限wxyz(java中使用setFilters)" /> </LinearLayout>