这里是我的方案,
我只是想要只数目和特定的特殊字符,像元在 EditText 字段的结尾。
前: 234.34 元
我不想在此输入后验证,但宁可用户只能输入十进制数和在结束了这个特殊的字符。
一些人请帮我来做这个。
解决方法 1:
您可以使用以下 (progromatically)
ed.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
ed.setKeyListener(DigitsKeyListener.getInstance("0123456789.$"));
您可以定义您想要在范围内的所有字符,DigitsKeyListener.getInstance
或者如果不想让用户输入一个 $ 符号和您想要在用户完成编辑后手动输入......
@Override
protected OnFocusChangeListener getOnFocusChangeListener() {
return new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
//Member variable in the class which contains an EditText
CurrencyTextbox.this.hadFocus = true;
} else {
// Thes EditText has lost focus
Log.v(TAG, "Lost focus.");
if (CurrencyTextbox.this.hadFocus) {
// We previously had focus, now we lost it, format the user input!
// Get current value of the Textbox
String value = CurrencyTextbox.this.textbox.getText().toString();
// Formatting the user input
value = String.format(//Doing some formatting);
// Reset the had focus
CurrencyTextbox.this.hadFocus = false;
}
}
}
};