我正在开发Android项目,我必须使用'mask'设置EditText。
这是浮动格式,但我有一个小问题。我不能输入0号码(例如我希望得到0,05)
现在它的工作方式如下:
任何帮助表示赞赏......
final EditText editText = new EditText(v.getContext());
editText.setLayoutParams(params);
editText.setText("");
editText.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLenght)});
editText.addTextChangedListener(new TextWatcher() {
int len=0;
@Override
public void afterTextChanged(Editable s) {
try {
String str = String.format("%,d", Long.parseLong(s.toString()
.replaceAll(",", "")));
String str = s.toString();
int pos = editText.getSelectionStart();
editText.removeTextChangedListener(this);
editText.setText(str);
editText.setSelection(pos + str.length() - s.length());
editText.addTextChangedListener(this);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
@Override
public void beforeTextChanged(CharSequence c, int arg1, int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});