美文网首页
自定义的只输入汉字和英文EditText,判断一个字符串是纯英文

自定义的只输入汉字和英文EditText,判断一个字符串是纯英文

作者: 头发依然在 | 来源:发表于2017-03-22 16:09 被阅读112次

//自定义EditText,只输入汉字和英文

/**
 * 自定义的只能输入汉字和英文的Edittext
 * 
 */

public class CustomEditText extends EditText {
    public CustomEditText(Context context) {
        super(context);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        return new mInputConnection(super.onCreateInputConnection(outAttrs), false);

    }


    class mInputConnection extends InputConnectionWrapper implements InputConnection {

        /**
         * Initializes a wrapper.
         * <p>
         * <p><b>Caveat:</b> Although the system can accept {@code (InputConnection) null} in some
         * places, you cannot emulate such a behavior by non-null {@link InputConnectionWrapper} that
         * has {@code null} in {@code target}.</p>
         *
         * @param target  the {@link InputConnection} to be proxied.
         * @param mutable set {@code true} to protect this object from being reconfigured to target
         *                another {@link InputConnection}.  Note that this is ignored while the target is {@code null}.
         */
        public mInputConnection(InputConnection target, boolean mutable) {
            super(target, mutable);
        }

        //拦截内容
        @Override
        public boolean commitText(CharSequence text, int newCursorPosition) {
            // 只能输入汉字或者英文
            if (!text.toString().matches("[\u4e00-\u9fa5]+") && !text.toString().matches("[a-zA-Z /]+")) {
                return false;
            }
            return super.commitText(text, newCursorPosition);
        }

        @Override
        public boolean sendKeyEvent(KeyEvent event) {
            return super.sendKeyEvent(event);
        }

        @Override
        public boolean setSelection(int start, int end) {
            return super.setSelection(start, end);
        }
    }
}

判断输入的内容是否是纯英文或纯汉字

//前提是传入的字符串只包含汉字和英文
 // 判断一个字符串0格式不纯   2是纯英文  或者1纯汉语
    public static int isChineseOrEngLish(String str) {
        if (str == null)
            return 0;
        //判断是否含有汉字
        Boolean isChinese = false;
        for (char c : str.toCharArray()) {
            if (isChinese(c)) {
                isChinese = true;// 有一个中文字符就返回
            }
        }
        //判断是否含有英文
        Boolean isLetter = false;
        for (char c : str.toCharArray()) {
            if (isLetter("" + c))
                isLetter = true;// 有一个中文字符就返回
        }
        if (isChinese && !isLetter) {//是纯汉字
            return 1;
        } else if (!isChinese && isLetter) {//是纯英文
            return 2;
        }
        return 0;
    }

    // 判断一个字符是否是中文
    public static boolean isChinese(char c) {
        return c >= 0x4E00 && c <= 0x9FA5;// 根据字节码判断
    }

    //判断一个字符是否是英文字母
    public static boolean isLetter(String c) {
        return c.matches("[a-zA-Z /]+");
    }

相关文章

网友评论

      本文标题:自定义的只输入汉字和英文EditText,判断一个字符串是纯英文

      本文链接:https://www.haomeiwen.com/subject/hkionttx.html