如何在android中验证编辑文本,使其最多可以包含6位数字和小数点后的2位数字?
我的要求是在编辑文本中添加过滤器,这样它可以包含最多6位数字和小数点后2位数字的最大值。我基本上需要的是允许用户最多输入6位数字,最多输入小数点后2位数字如何在android中验证编辑文本,使其最多可以包含6位数字和小数点后的2位数字?,android,validation,android-edittext,textwatcher,Android,Validation,Android Edittext,Textwatcher,我的要求是在编辑文本中添加过滤器,这样它可以包含最多6位数字和小数点后2位数字的最大值。我基本上需要的是允许用户最多输入6位数字,最多输入小数点后2位数字 For example 999999.99 is valid (6 digits and two digits after the decimal) 9999999 is not valid (& digits) 999.99 is valid (3 digits and 2 digits after the decimal
For example
999999.99 is valid (6 digits and two digits after the decimal)
9999999 is not valid (& digits)
999.99 is valid (3 digits and 2 digits after the decimal place)
现在,在谷歌搜索了一段时间后,我找到了一个解决方案,使用TextWatcher解决了以下代码的问题
public static CharSequence validText ="";
public static EditText setAmountValidation(final EditText editText,final Context context){
editText.addTextChangedListener(new TextWatcher()
{
private final Pattern sPattern = Pattern.compile("^([1-9][0-9]{0,5})?(\\.[0-9]{0,2}?)?$");
private CharSequence mText="";
private boolean isValid(CharSequence s) {
return sPattern.matcher(s).matches();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after){
if(isValid(s)){
validText= s;
}
mText = isValid(s) ? s : validText;
}
@Override
public void afterTextChanged(Editable s)
{
if (!isValid(s))
{
editText.setText(mText);
}
mText = "";
}
});
return editText;
}
}
现在,上面的代码抛出以下错误
Java堆栈溢出错误
我缺少的是什么?我认为您的问题是,您试图在您的
public void afterTextChanged(可编辑的)
中再次设置EditText,因为案例输入无效
如果您在PostExtChanged中更改文本,那么另一个PostExtChanged将被反复调用,就像您在logcat中看到的一样
另请参见,它们确实警告可能会在此处创建无限循环:
调用此方法是为了通知您,在s中的某个位置,文本已更改。从这个回调对s进行进一步的更改是合法的,但是要小心不要让自己陷入无限循环,因为您所做的任何更改都会导致再次递归调用这个方法
如果只允许小数点前6位和小数点后2位,则无需添加
addTextChangedListener
如果只需要8位(或
beforeDecimal=6,afterDecimal=2(或beforeDecimal=8,afterDecimal=0)您可以使用addTextChangedListener
EditText tv;
tv = (EditText) findViewById(R.id.test_value);
int beforeDecimal = 6, afterDecimal = 2;
tv.setFilters(new InputFilter[] { new DigitsKeyListener(Boolean.FALSE,
Boolean.TRUE) {
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
String etText = tv.getText().toString();
if (etText.isEmpty()) {
return null;
}
String temp = tv.getText() + source.toString();
if (temp.equals(".")) {
return "0.";
} else if (temp.toString().indexOf(".") == -1) {
if (temp.length() > beforeDecimal) {
return "";
}
} else {
int dotPosition;
int cursorPositon = tv.getSelectionStart();
if (etText.indexOf(".") == -1) {
dotPosition = temp.indexOf(".");
} else {
dotPosition = etText.indexOf(".");
}
if (cursorPositon <= dotPosition) {
String beforeDot = etText.substring(0, dotPosition);
if (beforeDot.length() < beforeDecimal) {
return source;
} else {
if (source.toString().equalsIgnoreCase(".")) {
return source;
} else {
return "";
}
}
} else {
temp = temp.substring(temp.indexOf(".") + 1);
if (temp.length() > afterDecimal) {
return "";
}
}
}
return super.filter(source, start, end, dest, dstart, dend);
}
} });
tv.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
String temp = tv.getText() + s.toString();
if (temp.contains(".")) {
beforeDecimal = 6; /***6 Digits beforedecimal and 2 after decimal****/
afterDecimal = 2;
} else {
beforeDecimal = 8; /***8 Digits beforedecimal and 0 after decimal***/
afterDecimal = 0;
}
}
编辑文本电视;
tv=(编辑文本)findViewById(R.id.test_值);
小数点前的整数=6,小数点后的整数=2;
tv.setFilters(新的InputFilter[]{new DigitsKeyListener(Boolean.FALSE,
Boolean.TRUE){
@凌驾
公共CharSequence筛选器(CharSequence源、int开始、int结束、,
跨越目的地、内部数据起点、内部数据起点){
字符串etText=tv.getText().toString();
if(etText.isEmpty()){
返回null;
}
String temp=tv.getText()+source.toString();
如果(温度等于(“.”){
返回“0”;
}else if(temp.toString().indexOf(“.”)=-1){
if(temp.length()>在十进制之前){
返回“”;
}
}否则{
整型点位;
int cursorposition=tv.getSelectionStart();
如果(etText.indexOf(“.”)=-1){
点位置=温度指数(“.”);
}否则{
dotPosition=etText.indexOf(“.”);
}
if(十进制后的光标位置){
返回“”;
}
}
}
返回超级过滤器(源、开始、结束、目标、开始、结束);
}
} });
tv.addTextChangedListener(新的TextWatcher(){
public void onTextChanged(字符序列,int start,int before,
整数计数){
String temp=tv.getText()+s.toString();
如果(温度包含(“.”){
beforeDecimal=6;/***十进制前6位,十进制后2位****/
小数点后=2;
}否则{
beforeDecimal=8;/***十进制前8位,十进制后0位***/
小数点后=0;
}
}
嘿,问题是如果我将我的三元条件更改如下:mText=isValid(s)?s:;一切正常。请尝试在isValid方法中记录要验证的字符序列。可能这不是您所期望的。