Android限定EditText的输入类型为数字或者英文(包括大小写)  // 监听密码输入框的输入内容类型,不可以输入中文.

其实有两种方案:
 1.在xml:EditText 设置属性----
android:digis="ABCDE1234567&"
其中"ABCDE1234567&"是你的限制规则。
例如:android:digits="0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

代码中就设置监听为:

et_content.setKeyListener(DigitsKeyListener.getInstance(digists));


如果是要求只能输入数字和小数点

et_content.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

此时要注意:只设置InputType.TYPE_NUMBER_FLAG_DECIMAL是无法实现只能输入数字和小数点的,必须InputType.TYPE_CLASS_NUMBER 和InputType.TYPE_NUMBER_FLAG_DECIMAL同时设置才可以输入。


2.EditText,TextView只能输入字母加数字,可在View控件后面加上监听器,如下

  tvPassword.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 edt) {                    try {                                 String temp = edt.toString();         String tem = temp.substring(temp.length() - 1, temp.length());                    char[] temC = tem.toCharArray();                                        int mid = temC[0];                    if (mid >= 48 && mid <= 57) {//数字                        return;                    }                    if (mid >= 65 && mid <= 90) {//大写字母                        return;                    }                                        if (mid > 97 && mid <= 122) {//小写字母                        return;                    }                    edt.delete(temp.length() - 1, temp.length());                } catch (Exception e) {                    //throw new Exception("登录页面监听密码输入框只能输入数字或者英文出错");                    // TODO: handle exception                }            }        });








1.EditText,TextView只能输入两位小数,先在XML文件里加上输入性:android:numeric="integer"//设置只能输入整数,如果是小数则是:decimal


然后在View空间后面加上监听器,如下

 

 EditText txtInput = (EditText) findViewById(R.id.txtInput);        txtInput.addTextChangedListener(new TextWatcher(){                        public void afterTextChanged(Editable edt){                                String temp = edt.toString();                int posDot = temp.indexOf(".");                if (posDot <= 0) return;                                if (temp.length() - posDot - 1 > 2) {                    edt.delete(posDot + 3, posDot + 4);                }           }                  public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}              public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}        });




EditText属性描述
 

android:layout_gravity="center_vertical"//设置控件显示的位置:默认top,这里居中显示,还有bottomandroid:background="@android:drawable/edit_text"这个属性,就可以回到原来的2.3式的风格了。android:hint="请输入数字!"//设置显示在空间上的提示信息android:numeric="integer"//设置只能输入整数,如果是小数则是:decimalandroid:maxLength="8"  //限制输入长度为8android:singleLine="true"//设置单行输入,一旦设置为true,则文字不会自动换行。android:gray="top" //多行中指针在第一行第一位置et.setSelection(et.length());//调整光标到最后一行android:autoText //自动拼写帮助android:capitalize //首字母大写android:digits //设置只接受某些数字android:singleLine //是否单行或者多行,回车是离开文本框还是文本框增加新行android:numeric="integer" //只接受数字 或者在java文件中写 numberText.setInputType(InputType.TYPE_CLASS_NUMBER);//只能输入数字android:password //密码android:phoneNumber // 输入电话号码android:editable //是否可编辑android:autoLink=”all” //设置文本超链接样式当点击网址时,跳向该网址android:password="true"//设置只能输入密码android:textColor = "#ff8c00"//字体颜色android:textStyle="bold"//字体,bold, italic, bolditalicandroid:textSize="20dip"//大小android:capitalize = "characters"//以大写字母写android:textAlign="center"//EditText没有这个属性,但TextView有android:textColorHighlight="#cccccc"//被选中文字的底色,默认为蓝色android:textColorHint="#ffff00"//设置提示信息文字的颜色,默认为灰色android:textScaleX="1.5"//控制字与字之间的间距android:typeface="monospace"//字型,normal, sans, serif, monospaceandroid:background="@null"//空间背景,这里没有,指透明android:layout_weight="1"//权重 在控制控件显示的大小时蛮有用的。android:textAppearance="?android:attr/textAppearanceLargeInverse"//文字外观,这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。可设置的值如下:textAppearanceButton、textAppearanceInverse、textAppearanceLarge、textAppearanceLargeInverse、textAppearanceMedium、textAppearanceMediumInverseandroid:textAppearance设置文字外观。如“?android:attr/textAppearanceLargeInverse”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。

 

参考文章:

https://www.cnblogs.com/weixing/p/3257058.html

更多相关文章

  1. Android(安卓)splitActionBarWhenNarrow ActionBar背景颜色设置
  2. 在android的Browser中设置User Agent
  3. android滚动条
  4. TextView和EidtText使用技巧
  5. View的xml的属性作用大剖析
  6. android设置Activity背景色为透明的2种方法
  7. 转-Android(安卓)UI学习 - Tab的学习和使用
  8. Android中的Shape使用总结
  9. Android设置TextView行间距(非行高)

随机推荐

  1. Android Studio推荐几个非常有用的工具
  2. Android自定义下拉刷新动画--仿百度外卖
  3. Android中可以做的两件坏事---破解锁屏密
  4. Android之UI学习篇一:TextVeiw如何显示丰
  5. 3D引擎--可移植到Android的开源的引擎及
  6. Android(安卓)远程图片获取和本地缓存(一)
  7. iOS 与 Android 之战,乔布斯是否正在重复
  8. Android(安卓)System.exit(code) and and
  9. Mono For Android(安卓)开发入门系列——
  10. [置顶] 随了Android的大流,站入Google阵营