用到EditText的方法setFilters過濾輸入的內容用法是et.setFilters(new InputFilter[]{lengthFilter});
private InputFilter lengthFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
// source:當前輸入的字符
// start:輸入字符的開始位置
// end:輸入字符的結束位置
// dest:當前已顯示的內容
// dstart:當前光標開始位置
// dent:當前光標結束位置
//Log.e("", "source=" + source + ",start=" + start + ",end=" + end + ",dest=" + dest.toString() + ",dstart=" + dstart + ",dend=" + dend);
if (dest.length() == 0 && source.equals(".")) {
return "0.";
}
String dValue = dest.toString();
String[] splitArray = dValue.split("\\.");
if (splitArray.length > 1) {
String dotValue = splitArray[1];
// if (dotValue.length() == 2) {//輸入框小數的位數是2的狀況,整個輸入框都不容許輸入
// return "";
// }
if (dotValue.length() == 2 && dest.length() - dstart < 3){ //輸入框小數的位數是2的狀況時小數位不能夠輸入,整數位能夠正常輸入
return "";
}
}
return null;
}
};ide