这是我的方案,
我只想在EditText字段的末尾只获取数字和特殊字符,例如$。
Ex:234.34 $
输入此输入后我不想验证,而是用户只能输入十进制数字,最后输入这个特殊字符。
有人请帮助我这样做。
答案 0 :(得分:2)
您可以使用以下(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;
}
}
}
};
答案 1 :(得分:0)
在编辑文字中添加属性android:inputType="numberDecimal"
答案 2 :(得分:0)
在EditText android:inputType="numberDecimal"
在类文件中,TextWatcher
添加EditText
,如以下代码,方法afterTextChanged(Editable s)
在EditText文本末尾添加“$”。
EditText input = (EditText) findViewById(R.id.search_text_box);
input.addTextChangedListener(onInputTextChanged);
public TextWatcher onInputTextChanged = new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void afterTextChanged(Editable s)
{
// Add "$" at the end of text in EditText
}
};
答案 3 :(得分:0)
您只能使用Java代码接受电话号码和电话号码类型
EditText number1 = (EditText) layout.findViewById(R.id.edittext);
number1.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
number1.setKeyListener(DigitsKeyListener.getInstance("Replace with your special characters”));
此代码将避免在读取输入后进行大量验证