限定EditText只能输入两位小数的两种方式

  1. /** 
  2.  * 限定EditText只能输入最多两位小数 
  3.  * @author Administrator 
  4.  * 
  5.  */  
  6. public class MainActivity extends ActionBarActivity {  
  7.   
  8.     private EditText num_et;  
  9.     private EditText num_et2;  
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         num_et = (EditText) findViewById(R.id.num_et);  
  16.         num_et2 = (EditText) findViewById(R.id.num_et2);  
  17.         /** 
  18.          * 第一种方法 
  19.          */  
  20.         num_et.addTextChangedListener(new TextWatcher() {  
  21.                
  22.             @Override  
  23.             public void onTextChanged(CharSequence s, int start, int before,  
  24.                     int count) {  
  25.                 if (s.toString().contains(".")) {  
  26.                     if (s.length() - 1 - s.toString().indexOf(".") > 2) {  
  27.                         s = s.toString().subSequence(0,  
  28.                                 s.toString().indexOf(".") + 3);  
  29.                         num_et.setText(s);  
  30.                         num_et.setSelection(s.length());  
  31.                     }  
  32.                 }  
  33.                 if (s.toString().trim().substring(0).equals(".")) {  
  34.                     s = "0" + s;  
  35.                     num_et.setText(s);  
  36.                     num_et.setSelection(2);  
  37.                 }  
  38.    
  39.                 if (s.toString().startsWith("0")  
  40.                         && s.toString().trim().length() > 1) {  
  41.                     if (!s.toString().substring(12).equals(".")) {  
  42.                         num_et.setText(s.subSequence(01));  
  43.                         num_et.setSelection(1);  
  44.                         return;  
  45.                     }  
  46.                 }  
  47.             }  
  48.    
  49.             @Override  
  50.             public void beforeTextChanged(CharSequence s, int start, int count,  
  51.                     int after) {  
  52.    
  53.             }  
  54.    
  55.             @Override  
  56.             public void afterTextChanged(Editable s) {  
  57.                 // TODO Auto-generated method stub  
  58.                    
  59.             }  
  60.    
  61.         });  
  62.         /** 
  63.          * 第二种方法 
  64.          */  
  65.         num_et2.addTextChangedListener(new TextWatcher()   
  66.           {  
  67.               public void afterTextChanged(Editable edt)   
  68.               {  
  69.                   String temp = edt.toString();  
  70.                   int posDot = temp.indexOf(".");  
  71.                   if (posDot <= 0return;  
  72.                   if (temp.length() - posDot - 1 > 2)  
  73.                   {  
  74.                       edt.delete(posDot + 3, posDot + 4);  
  75.                   }  
  76.               }  
  77.   
  78.               public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}  
  79.   
  80.               public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}  
  81.           });  
  82.     }  
  83. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值