我使用过滤器来设置editText的长度
我将EditText长度设置为10,如下所示。
TextView editVew = new TextView(R.id.txtAmt);
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(10);
editVew.setFilters(FilterArray);
editVew.setInputType(InputType.TYPE_CLASS_NUMBER);
我的困惑:如何在我的编号中设置2位数的分数,我的编辑视图的总长度不应超过10位?
如果有人知道请回复。
感谢
答案 0 :(得分:1)
尝试此操作以使您的edittext支持小数点后的两位数。
EditText text = (EditText) findViewById(R.id.txtAmt);
text.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable edittext)
{
String str= edittext.toString();
int posDot = str.indexOf(".");
if (posDot <= 0) return;
if (str.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) {}
});
在xml中使用android:maxLength="10"
来限制editText支持10个最大输入数字
答案 1 :(得分:0)
试试这个..
maxLength = 10;
smsType = "free";
FilterArraySeventy[0] = new InputFilter.LengthFilter(maxLength);
message.setFilters(FilterArrayten);
答案 2 :(得分:0)
您的答案的解决方案
amountEditText.setRawInputType(Configuration.KEYBOARD_12KEY);
amountEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))
{
String userInput= ""+s.toString().replaceAll("[^\\d]", "");
StringBuilder cashAmountBuilder = new StringBuilder(userInput);
while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
cashAmountBuilder.deleteCharAt(0);
}
while (cashAmountBuilder.length() < 3) {
cashAmountBuilder.insert(0, '0');
}
cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
cashAmountBuilder.insert(0, '$');
amountEditText.setText(cashAmountBuilder.toString());
// keeps the cursor always to the right
Selection.setSelection(amountEditText.getText(), cashAmountBuilder.toString().length());
}
}
});
这里讨论了同样的问题