editText限制输入的4种方法

来源:互联网 发布:翻译字幕的软件 编辑:程序博客网 时间:2024/05/19 11:47
方式1:xml中配置inputType。 常用于限制为 Date,time,number,Email,phone等常用的格式
方式2:xml中配置digits。可以自定义限制的区间。
方式3:java中使用setKeyListener,添加DigitsKeyListener。(方法2就是最终就是通过该方法实现)
方法4:java中使用setFilters,添加InputFilter。可以在回调函数filter中自己写处理,最后返回处理过的CharSequence对象。

方法: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";@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count) {}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void afterTextChanged(Editable s) {// TODO Auto-generated method stubStringBuffer 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=" .@~-,:*?!#/\\=+?^;%$()[]{}|`&lt;&gt;&amp;&quot;&apos;_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>



2 0
原创粉丝点击