2010-11-02 82 views
3

有什么办法可以限制在运行系统中只有2位数字可以输入EditTextAndroid中的EditText?

例如:

如果我把我的android:inputType="numberDecimal",我进入它的价值。它接受值123.000000000000。但我想限制它像123.00

有没有任何可能的方法来做到这一点?

回答

6

看看下面的文章,可能的一些帮助你:

Preventing input

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) {} 
});