Edittext帮助类,常用的一些方法

/**
 * 显示或者隐藏密码
 */
public static void toggleShowOrHideInput(EditText edt) {
    TransformationMethod transformationMethod = edt.getTransformationMethod();
    if (transformationMethod instanceof HideReturnsTransformationMethod) {
        edt.setTransformationMethod(PasswordTransformationMethod.getInstance());
    } else {
        edt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
    }

    setSelectPosition(edt);
}


public static void hideInput(EditText edt) {
    edt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}

public static void hideOrShowSoftKeyboard() {
    InputMethodManager imm = (InputMethodManager) Utils
            .getInstance().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    assert imm != null;
    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}

/**
 * 隐藏软键盘
 */
public static void hideSoftKeyboard(View view) {
    InputMethodManager imm = (InputMethodManager) Utils.getInstance().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    assert imm != null;
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

/**
 * 设置光标显示位置
 */
public static void setSelectPosition(EditText edt) {
    String txt = edt.getText().toString().trim();
    if (!TextUtils.isEmpty(txt)) {
        edt.setSelection(txt.length());
    }
}

/**
 * 判断是否是表情
 *
 * @param codePoint
 * @return
 */
public 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));
}

/**
 * 判别是否包含Emoji表情
 */
public static boolean containsEmoji(String str) {
    int len = str.length();
    for (int i = 0; i < len; i++) {
        if (isEmojiCharacter(str.charAt(i))) {
            return true;
        }
    }
    return false;
}

/**
 * 判别是否包含Emoji表情
 *
 * @return
 */
public static boolean hasEmoji(String content) {
    Pattern pattern = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]");
    Matcher matcher = pattern.matcher(content);
    if (matcher.find()) {
        return true;
    }
    return false;
}

/**
 * 判别是否包含Emoji表情
 *
 * @return
 */
public static boolean withEmoji(String content) {
    Pattern pattern = Pattern.compile("" +
            "(?:[\\uD83C\\uDF00-\\uD83D\\uDDFF]|[\\uD83E\\uDD00-\\uD83E\\uDDFF]|" +
            "[\\uD83D\\uDE00-\\uD83D\\uDE4F]|[\\uD83D\\uDE80-\\uD83D\\uDEFF]|" +
            "[\\u2600-\\u26FF]\\uFE0F?|[\\u2700-\\u27BF]\\uFE0F?|\\u24C2\\uFE0F?|" +
            "[\\uD83C\\uDDE6-\\uD83C\\uDDFF]{1,2}|" +
            "[\\uD83C\\uDD70\\uD83C\\uDD71\\uD83C\\uDD7E\\uD83C\\uDD7F\\uD83C\\uDD8E\\uD83C\\uDD91-\\uD83C\\uDD9A]\\uFE0F?|" +
            "[\\u0023\\u002A\\u0030-\\u0039]\\uFE0F?\\u20E3|[\\u2194-\\u2199\\u21A9-\\u21AA]\\uFE0F?|" +
            "[\\u2B05-\\u2B07\\u2B1B\\u2B1C\\u2B50\\u2B55]\\uFE0F?|[\\u2934\\u2935]\\uFE0F?|" +
            "[\\u3030\\u303D]\\uFE0F?|[\\u3297\\u3299]\\uFE0F?|" +
            "[\\uD83C\\uDE01\\uD83C\\uDE02\\uD83C\\uDE1A\\uD83C\\uDE2F\\uD83C\\uDE32-\\uD83C\\uDE3A\\uD83C\\uDE50\\uD83C\\uDE51]\\uFE0F?|" +
            "[\\u203C\\u2049]\\uFE0F?|[\\u25AA\\u25AB\\u25B6\\u25C0\\u25FB-\\u25FE]\\uFE0F?|" +
            "[\\u00A9\\u00AE]\\uFE0F?|[\\u2122\\u2139]\\uFE0F?|\\uD83C\\uDC04\\uFE0F?|\\uD83C\\uDCCF\\uFE0F?|" +
            "[\\u231A\\u231B\\u2328\\u23CF\\u23E9-\\u23F3\\u23F8-\\u23FA]\\uFE0F?)");
    Matcher matcher = pattern.matcher(content);
    if (matcher.find()) {
        return true;
    }
    return false;
}


/**
 * 禁止EditText输入表情
 *
 * @param editText
 * @param maxLength 输入长度限制(因为设置InputFilter会导致maxLength属性失效)
 */
public static void limitEditTextInputEmoji(EditText editText, int maxLength) {
    //表情过滤器
    InputFilter emojiFilter = (source, start, end, dest, dstart, dend) -> {
        Pattern emoji = Pattern.compile(
                "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        Matcher emojiMatcher = emoji.matcher(source);
        if (emojiMatcher.find()) return "";
        else return null;
    };

    if (maxLength != -1) {//-1表示不限制
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength), emojiFilter});
    } else {
        editText.setFilters(new InputFilter[]{emojiFilter});
    }
}

/**
 * 禁止EditText输入特殊字符
 *
 * @param editText
 */
public static void limitEditTextInputSpeChat(EditText editText) {
    InputFilter filter = (source, start, end, dest, dstart, dend) -> {
        String speChat = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
        Pattern pattern = Pattern.compile(speChat);
        Matcher matcher = pattern.matcher(source.toString());
        if (matcher.find()) return "";
        else return null;
    };
    editText.setFilters(new InputFilter[]{filter});
}

/**
 * 验证URL地址
 *
 * @param url 格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或 http://www.csdn.net:80
 * @return 验证成功返回true,验证失败返回false
 */
public static boolean checkURL(String url) {
    String regex = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;*]+[-A-Za-z0-9+&@#/%=~_|]";
    return Pattern.matches(regex, url);
}

你可能感兴趣的:(Edittext帮助类,常用的一些方法)