Android edittext字符串提醒处理,及是否包含表情包及标点符号,设置edit光标位置


edt_zit.setSelection(edt_content.length());


   /**
     * 现在是记录上一次显示的字符串,设置为当前不符合法输入的字符串内容
     * */
  import_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                checkEdittextContent();

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str=import_name.getText().toString();
                Log.e("tag", "onEditorAction: "+str );
                if (!TextStyleUtils.isLegal(str)){
                    showToast("节点名称不能为特殊符号");
                    if (str.length()>1) {
//                        str=str.substring(0, str.length() - 1);
                        import_name.setText(edtNameLast);
                        import_name.setSelection(edtNameLast.length());
                    }else {
                        import_name.setText("");
                        edtNameLast=import_name.getText().toString();
                    }
                }

                if (str.length()>6){
                    showToast("节点名称为1-6位");
                    import_name.setText(edtNameLast);
                    import_name.setSelection(str.length());
                }
                edtNameLast=import_name.getText().toString();

            }
        });
//true为含有表情
 private static boolean noContainsEmoji(String str) {
        int len = str.length();
        for (int i = 0; i < len; i++) {
            if (isEmojiCharacter(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    private  static boolean isEmojiCharacter(char codePoint) {
        return !((codePoint == 0x0) ||
                (codePoint == 0x9) ||
                (codePoint == 0xA) ||
                (codePoint == 0xD) ||
                ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
                ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
    }


    /**
     * 判断是否有符号、空格
     * 合法则true,不合法false
     * */
    public static boolean isLegal(String name){
        if (TextStyleUtils.noContainsEmoji(name)){
            return false;
        }
        String str="~ !@#$%^&*()_+,./;'[],。、;‘《》?:“|{}<>?:|";
        for (int i=0;i<name.length();i++){
            if (str.contains(name.substring(i,i+1))){
                return false;
            }
        }
        return true;
    }

    /**
     * true为含表情
     * */
    private static boolean noContainsEmoji(String str) {//真为含有表情
        int len = str.length();
        for (int i = 0; i < len; i++) {
            if (isEmojiCharacter(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    private  static boolean isEmojiCharacter(char codePoint) {
        return !((codePoint == 0x0) ||
                (codePoint == 0x9) ||
                (codePoint == 0xA) ||
                (codePoint == 0xD) ||
                ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
                ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值