整理的常用的正则和屏幕判断的工具类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34942689/article/details/80182172
public class SmartCommon {
/**
 * 判断是否为手机号
 *
 * @param mobiles
 * @return 验证通过返回true
 */
public static boolean isMobileNO(String mobiles) {
    Pattern p = null;
    Matcher m = null;
    boolean b = false;
    p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号
    m = p.matcher(mobiles);
    b = m.matches();
    return b;
}

/**
 * 判断是否是邮箱
 * @param email
 * @return
 */

public static boolean isEmail(String email) {
    String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    Pattern p = Pattern.compile(str);
    Matcher m = p.matcher(email);
    return m.matches();
}

/**
 * 是否以zh 结尾
 * @param context
 * @return
 */
public static boolean isZh(Context context) {
    Locale locale = context.getResources().getConfiguration().locale;
    String language = locale.getLanguage();
    if (language.endsWith("zh"))
        return true;
    else
        return false;
}
/**
 * 屏幕常亮
 *
 * @param acitivity
 * @return
 */
public static void powerManager(Activity acitivity) {
    PowerManager pm = (PowerManager) acitivity.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");
    // in onResume() call
    mWakeLock.acquire();
    // in onPause() call
    mWakeLock.release();
}

/**
 * 屏幕常亮
 *
 * @param acitivity
 * @return
 */
public static void closePowerManager(Activity acitivity) {
    PowerManager pm = (PowerManager) acitivity.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock mWakeLock = pm.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "My Tag");
    // in onResume() call
    mWakeLock.acquire();
    // in onPause() call
    mWakeLock.release();
}

/**
 * 字符串必须是a-z A-Z _ 0-9中的一种
 * @param string
 * @return
 */
public static boolean isConSpeCharacters2(String string) {
    String regex = "^[_0-9a-zA-Z]$";
    return string.matches(regex);
}

/**
 * 有汉字返回true
 * @param string
 * @return
 */
public static boolean isConSpeCharacters1(String string) {
    String regex = "[^\u4e00-\u9fa5]{6,18}";
    return string.matches(regex);
}

/**
 * 不能全为字母或汉字的包含  0-9 a-z A-Z的6-18位字符串
 * @param string
 * @return
 */
public static boolean isConSpeCharacters3(String string) {
    String regex = "^((?=.*[A-Za-z].*)(?=.*[0-9].*))[0-9A-Za-z]{6,18}$";
    return string.matches(regex);
}

/**
 * 电话号码验证
 *
 * @param tel
 * @return 验证通过返回true
 */
public static boolean isPhone(String tel) {
    Pattern p1 = null, p2 = null, p3 = null;
    Matcher m = null, n = null;
    boolean b = false;

    p1 = Pattern.compile("(\\(\\d{3,4}\\)|\\d{3,4}-|\\s)?\\d{8}"); // 验证带区号的
    p3 = Pattern.compile("(\\(\\d{3,4}\\)|\\d{3,4}|\\s)?\\d{8}"); // 验证带区号的,不带-

    p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 验证没有区号的
    if (tel.length() > 9) {
        m = p1.matcher(tel);
        n = p3.matcher(tel);
        if (m.matches() || n.matches()) {
            b = true;
        } else {
            b = false;
        }
    } else {
        m = p2.matcher(tel);
        b = m.matches();
    }
    return b;
}

// 中文编码
public static String encodeString(String str) {
    try {
        return URLEncoder.encode(str, "utf-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return "";
}

// 判断字符串是否为空
public static boolean isEmpty(String source) {
    return isNullOrEmpty(source);
}

/**
 * 电话号码验证
 *
 * @param str
 * @return 验证通过返回true
 */
public static boolean isTel(String str) {
    Pattern p1 = null, p2 = null;
    Matcher m = null;
    boolean b = false;
    p1 = Pattern.compile("^[0][1-9]{2,3}[0-9]{5,10}$");  // 验证带区号的  
    p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         // 验证没有区号的  
    if (str.length() > 9) {
        m = p1.matcher(str);
        b = m.matches();
    } else {
        m = p2.matcher(str);
        b = m.matches();
    }
    return b;
}


/**
 * 判断是否有汉字
 *
 * @param string 给定的字符串
 * @return 长度
 */
public static boolean isCh(String string) {
    char[] chars = string.toCharArray();
    for (int w = 0; w < string.length(); w++) {
        char ch = chars[w];
        if (ch >= '\u0391' && ch <= '\uFFE5') {
            return true;
        }
    }
    return false;
}

//首字符是否为字母
public static boolean check(String fstrData) {
    char c = fstrData.charAt(0);
    if (((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {
        return true;
    } else {
        return false;
    }
}

public static boolean isNullOrEmpty(String source) {
    return null == source || 0 == source.trim().length();
}


public static boolean isScreenChange(Context context) {
    Configuration mConfiguration = context.getResources().getConfiguration(); // 获取设置的配置信息
    int ori = mConfiguration.orientation; // 获取屏幕方向

    if (ori == Configuration.ORIENTATION_LANDSCAPE) {
        // 横屏
        return true;
    } else if (ori == Configuration.ORIENTATION_PORTRAIT) {
        // 竖屏
        return false;
    }
    return false;
}

public static boolean getNetworkType(Context context) {
    NetworkInfo.State wifiState = null;
    NetworkInfo.State mobileState = null;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();

    if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState
            && NetworkInfo.State.CONNECTED != mobileState) {
        return false;
    } else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
        return false;
    } else if (mobileState != null && NetworkInfo.State.CONNECTED == mobileState) {
        return true;
    }
    return false;
}



/**
 * 验证ip是否合法
 *
 * @param text ip地址
 * @return 验证信息
 */
public static boolean ipCheck(String text) {
    if (text != null && !text.isEmpty()) {
        // 定义正则表达式
        String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
        // 判断ip地址是否与正则表达式匹配
        if (text.matches(regex)) {
            // 返回判断信息
            return true;
        } else {
            // 返回判断信息
            return false;
        }
    }
    // 返回判断信息
    return false;
}

/**
 * 子网掩码是否合法
 *
 * @param text
 * @return 验证信息
 */
public static boolean netmaskCheck(String text) {
    if (text != null && !text.isEmpty()) {
        // 定义正则表达式
        String regex1 = "255\\.255\\.255\\.(0|128|192|224|240|248|252|254|255)";
        String regex2 = "255\\.255\\.(0|128|192|224|240|248|252|254|255)\\.0";
        String regex3 = "255\\.(0|128|192|224|240|248|252|254|255)\\.0\\.0";
        // 判断ip地址是否与正则表达式匹配
        if (text.matches(regex1) || text.matches(regex2) || text.matches(regex3)) {
            // 返回判断信息
            return true;
        } else {
            // 返回判断信息
            return false;
        }
    }
    // 返回判断信息
    return false;
}
public static int getPageCount(int pageSize, int count) {
    if (count % pageSize == 0) {
        return count / pageSize;
    } else {
        return (count / pageSize) + 1;
    }
}
   }

猜你喜欢

转载自blog.csdn.net/qq_34942689/article/details/80182172