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) {}
});
android:inputType="numberDecimal"
http://wang-peng1.iteye.com/blog/818417
代码中设置输入最大长度
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});//设置输入最大长度
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});//设置输入最大长度