Java过滤器InputFilter

1.只能输入数字

public class NumberOnlyFilter implements InputFilter 

{
@Override
/*返回"" 禁止输入
*返回null 不限制输入 


* */
public CharSequence filter(CharSequence source, int start, int end,Spanned dest, int dstart, int dend ) 
{
for (int i = start; i < end; i++) 
{
if(source.charAt(i)<48||source.charAt(i)>57)//数字范围48~57 decimal
{
return dest.subSequence(dstart, dend);
}
}
return null;
}
}
2.只能输入数字和小数点
public class IpFilter implements InputFilter 
{


@Override
public CharSequence filter(CharSequence source, int start, int end,Spanned dest, int dstart, int dend ) 
{
for (int i = start; i < end; i++) 
{
if(source.charAt(i)=='/'||source.charAt(i)<46||source.charAt(i)>57)//数字范围48~57 decimal
{
return dest.subSequence(dstart, dend);
}
}
return null;
}
}

你可能感兴趣的:(Java,Android)