我有一个编辑文本,用户可以在其中输入金额,我希望对编辑文本有一些限制
- 金额不能大于100000
- 用户输入的金额最多保留两位小数,例如 99999.50
-已编辑-已解决
我如何实现这一点,我是否必须创建一个filter
或者我可以使用Regular Expression
inmyEdittext.addTextChangedListener(new TextWatcher() ...
myAmountEditText.addTextChangedListener(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) {
if(amount!=null && amount.length()>=1 ){
if(amount.contains(".")){
int indexOFdec = amount.indexOf(".");
if(indexOFdec >=0) {
if(amount.substring(indexOFdec).length() >3){
amount = amount.substring(0, amount.length()-1);
myAmountEditText.setText(amount);
myAmountEditText.setSelection(myAmountEditText.getText().length());
}
}
}
Double a = Double.parseDouble(amount.toString());
if(a>0 && a<100000.00){
//my stuff
}else{
Toast.makeText(context, "amount greater than 100000", Toast.LENGTH_SHORT).show();
}
}
});