自定义editext自定义Filte只能输入汉字,禁止输入特殊字符,空格限制小数长度,字母大小写互转

/**

  • editext 常用相关工具
    */
    public class EditUtil {

     // 设置只能输入汉字
    

    public static void setInPutNameFilter(EditText et,int length){
    InputFilter filter = new InputFilter() {
    @Override
    public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
    String speChat="[^\u4E00-\u9FA5]";
    Pattern pattern = Pattern.compile(speChat);
    Matcher matcher = pattern.matcher(charSequence.toString());
    if(!matcher.find()){
    return null;
    }else {
    return “”;
    }
    }
    };
    et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length),filter});
    }

    /**

    • 禁止EditText输入特殊字符和表情、空格、最大长度
    • @param editText
    • @param maxLength 输入框最大输入字符数
      /
      public static void setEditTextInhibitInputSpeChat(EditText editText, int maxLength){
      InputFilter filter=new InputFilter() {
      @Override
      public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
      String speChat="[`!@#$%^&*()+=|{}’:;’,\[\].<>/?!@#¥%……&
      ()——+|{}【】‘;:”“’。,、?]|[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]";
      Pattern pattern = Pattern.compile(speChat);
      Matcher matcher = pattern.matcher(source.toString());
      if(matcher.find() || source.equals(" ")){
      return “”;
      }else {
      return null;
      }
      }
      };
      editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength), filter});
      }

    //原本输入的小写字母转喂大写字母
    public static class TransInformation extends ReplacementTransformationMethod {
    /**
    * 原本输入的小写字母
    */
    @Override
    protected char[] getOriginal() {
    return new char[]{‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’};
    }

     /**
      * 替代为大写字母
      */
     @Override
     protected char[] getReplacement() {
         return new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
     }
    

    }

}

/**
*

  • 限制输入框输入小数
  • 默认第一位输入小数点时,转换为0.
  • 如果起始位置为0,且第二位跟的不是".",则无法后续输入

*/
public class EditDoubleWather implements TextWatcher{
private EditText editText;
private int length = 0; // 小点长度

public EditDoubleWather(EditText et) {
    editText = et;
}
public EditDoubleWather setLength(int d) {
    length = d;
    return this;
}


@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    String trim = s.toString().trim();
    if (!TextUtils.isEmpty(trim)) {
        if (trim.contains(".")) {
            String[] split = trim.split("\\.");
            if (split.length > 1) {
                if (StringUtil.StrCheck(split[1]) && split[1].length() > length) {
                    String d;
                    if (!StringUtil.StrCheck(split[0])) {
                        d = "0." + split[1].substring(0, length);
                    }else {
                        d = split[0] + "." + split[1].substring(0, length);
                    }
                    editText.setText(d);
                    editText.setSelection(d.length());
                }
            }
        }
    }


}

@Override
public void afterTextChanged(Editable editable) {

}

}

/**
* double 转换
* @param d 字符串
* @param lenght 保留小数长度
* @return
*/
public static double saveDouble(String d,int lenght){
if (d.substring(0,1).equals(".")) {
d = “0”+d;
}
BigDecimal bigDecimal = new BigDecimal(d);
return bigDecimal.setScale(lenght,BigDecimal.ROUND_HALF_UP).doubleValue();
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值