EditView不可编辑状态和可编辑状态动态切换 及 EditView的其它特效



不可编辑状态

两种选择:

第一个 不可编辑但是会弹出输入法

android:editable="false" 

 

第二个 不可编辑同时不会弹出输入法
android:focusable="false"

代码动态控制:

editText.setFocusable(false);和editText.setEnabled(false);时不可编辑,但是editText.setFocusable(true);和 editText.setEnabled(true);也是不可编辑的,感觉这个时候EditText控件高亮度了,但是没有焦点

最后尝试使用editText.setFocusable(false);和editText.setFocusableInTouchMode(false);设置不可编辑状态;editText.setFocusableInTouchMode(true);editText.setFocusable(true);editText.requestFocus();设置可编辑状态

这个可以实现可编辑和不可编辑,但是又发现一个问题,在不可编辑状态如果常按住控件,可以进行粘帖操作,不知道怎么可以不能进行粘帖

经测试发现,如果EditText预先有内容,光标自然会在文字的末尾,但是如果预先内容为空,然后设置好内容,这种情况下光标自然会在文字的开头,所以这种情况下可以这样做让光标位于末尾:


editText.setText("0");

editText.requestFocus();  


android:background="#00000000"//不要文本框背景 

EditText字数限制的设置

(1)在.xml中设置:android:maxLength=“50” 


如何设置EditText,使得只能输入数字或者某些字母呢?

一、设置EditText,只输入数字:

方法1:直接生成DigitsKeyListener对象就可以了。

et_1.setKeyListener(new DigisKeyListener(false,true));

方法2:在EditText中设置属性,android:numeric="integer"即只能输入整数,如下

<EditText

android:singleLine="true"

android:numeric="integer"

/>

方法3:新建一个char[],在里面添加允许输入的字符。如下

editText.setKeyListener(new NumberKeyListener(){
      protected char[] getAcceptedChars(){
       return new char[]{'1','2','3','4','5','6','7','8','9','0'};
      }
   public int getInputType() {
    return android.text.InputType.TYPE_CLASS_PHONE;  
   }
  });

二、设置EditText只能输入某些字母,如下面设置edtitext只能输入A—N,a—n这些字母。方法如下:

editText.setKeyListener(new NumberKeyListener(){

protected char[] getAcceptedChars()

{

char[] numberChars[]={'a,'b','c','d','e','f','A','B','C','D'};

return numberChars;

}

});


更多参考:http://www.cnblogs.com/weixing/p/3257058.html
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值