在我们做项目的过程中,总是会有使用到各种工具类,比如:验证邮箱、手机号、身份证、IP地址等等,这里把平时可能常用的一些进行了整合,写在下面,方便大家查阅!
/**
* 手机号验证
*
* @param str
* @return 验证通过返回true
*/
public static boolean isMobile(String str) {
if (str == null)
return false;
return Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$").matcher(str).matches(); // 验证手机号
}
/**
* 邮箱验证
*
* @param str
* @return 验证通过返回true
*/
public static boolean isEmail(String str) {
if (str == null)
return false;
return Pattern
.compile("^([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})(\\]?)$")
.matcher(str).matches();
}
/**
* 电话号码验证
*
* @param str
* @return 验证通过返回true
*/
public static boolean isPhone(String str) {
if (str == null)
return false;
Pattern p1 = null, p2 = null;
Matcher m = null;
boolean b = false;
str = str.replaceAll("-", "");
if (str.length() == 11) {
p1 = Pattern.compile("^[0][1-9]{2,3}[0-9]{5,10}$"); // 验证带区号的
m = p1.matcher(str);
b = m.matches();
} else if (str.length() <= 9) {
p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 验证没有区号的
m = p2.matcher(str);
b = m.matches();
}
if (!b)
return isMobile(str);
return b;
}
/**
* 把字体结果dimen转化成原sp值
*
* @return
*/
public static float floatToSpDimension(float value, Context context) {
return value / context.getResources().getDisplayMetrics().scaledDensity;
}
@SuppressWarnings({"unchecked"})
public static <T> T getView(View v, int resId) {
return (T) v.findViewById(resId);
}
/**
* 获取当前时间Date
*
* @return 现在时间(Now)
*/
public static String getNowTime() {
Date d = new Date(System.currentTimeMillis());
String type = "HH:mm:ss";
SimpleDateFormat formatter = new SimpleDateFormat(type, Locale.CHINA);
return formatter.format(d);
}
/**
* 获取当前时间Date
*/
public static String getDateTime(long ltime) {
return getDateTime(ltime, null);
}
/**
* 获取当前时间Date
*/
public static String getDateTime(long ltime, String type) {
if ((ltime + "").length() == 10) ltime = ltime * 1000L;
if (type == null) type = "yyyy-MM-dd HH:mm:ss";
Date d = new Date(ltime);
SimpleDateFormat formatter = new SimpleDateFormat(type, Locale.CHINA);
return formatter.format(d);
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 将px值转换为sp值,保证文字大小不变
*
* @param pxValue
* @return
*/
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 将sp值转换为px值,保证文字大小不变
*
* @param spValue
* @return
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
/**
* 获取控件的高度,如果获取的高度为0,则重新计算尺寸后再返回高度
*
* @param view
* @return
*/
public static int getViewMeasuredHeight(View view) {
calcViewMeasure(view);
return view.getMeasuredHeight();
}
/**
* 获取控件的宽度,如果获取的宽度为0,则重新计算尺寸后再返回宽度
*
* @param view
* @return
*/
public static int getViewMeasuredWidth(View view) {
calcViewMeasure(view);
return view.getMeasuredWidth();
}
/**
* 测量控件的尺寸
*
* @param view
*/
public static void calcViewMeasure(View view) {
int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
view.measure(width, expandSpec);
}
/**
* 返回当前程序版本信息
*/
public static PackageInfo getAppVersion(Context context) {
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
} catch (Exception e) {
Debug.e("VersionInfo|Exception:" + e);
}
return null;
}
/**
* 检测该包名所对应的应用是否存在
*
* @param context
* @param packageName
* @return
*/
public static boolean checkPackage(Context context, String packageName) {
if (TextUtils.isEmpty(packageName))
return false;
try {
context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_ACTIVITIES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
/**
* 网络是否连接
*
* @param context
* @return
*/
public static boolean isNetworkConnected(Context context) {
return new MyShare(context.getApplicationContext()).getInt(Constant.NET_STATUS) > -1;
}
private static long lastTime = 0;
/**
* 是否是快速点击
*
* @return
*/
public static boolean isFastClick() {
long curTime = System.currentTimeMillis();
if (curTime - lastTime < 500)
return true;
lastTime = curTime;
return false;
}
/**
* 是否是车牌号
*
* @param str
* @return
*/
public static boolean isCarNumber(String str) {
if (isNoEmpty(str))
return Pattern.compile("^[\u4e00-\u9fa5|A-Z]{1}[A-Z]{1}[A-Z_0-9]{5}$").matcher(str).matches();
return false;
}
public static final String[] weeks = {};
/**
* 获取周几
*
* @param week
* @return
*/
public static String getWeekName(int week) {
switch (week) {
case Calendar.SUNDAY:
return "周日";
case Calendar.MONDAY:
return "周一";
case Calendar.TUESDAY:
return "周二";
case Calendar.WEDNESDAY:
return "周三";
case Calendar.THURSDAY:
return "周四";
case Calendar.FRIDAY:
return "周五";
case Calendar.SATURDAY:
return "周六";
}
return null;
}
/**
* @return null may be returned if the specified process not found
*/
public static String getProcessName(Context cxt, int pid) {
ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
if (runningApps == null) {
return null;
}
for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
if (procInfo.pid == pid) {
return procInfo.processName;
}
}
return null;
}
public static boolean isNoEmpty(String str) {
return !isEmpty(str);
}
public static boolean isEmpty(String str) {
if (null == str)
return true;
if (str.length() == 0)
return true;
if (str.trim().length() == 0)
return true;
if (str.indexOf("null") == 0)
return true;
return false;
}
public static boolean isNoEmpty(List<?> datas) {
return !isEmpty(datas);
}
public static boolean isEmpty(List<?> datas) {
if (datas == null)
return true;
if (datas.size() == 0)
return true;
return false;
}
/**
* 去掉多余的0
*
* @param str
* @return
*/
public static String removeNumberZero(String str) {
if (isEmpty(str)) {
return "0";
}
if (str.indexOf(".") > 0) {
str = str.replaceAll("0+?$", "");// 去掉多余的0
str = str.replaceAll("[.]$", "");// 如最后一位是.则去掉
}
return str;
}
/**
* 是否是数字
*
* @param str
* @return 验证通过返回true
*/
public static boolean isNumber(String str) {
if (str == null)
return false;
return Pattern.compile("[0-9]+").matcher(str).matches();
}
/**
* 是否是中文
*
* @param str
* @return
*/
public static boolean isChinese(String str) {
if (str == null)
return false;
return Pattern.compile("^[\u4e00-\u9fa5]+$").matcher(str).matches();
}
/**
* 是否是IP地址
*
* @param str
* @return
*/
public static boolean isIpAddress(String str) {
if (str == null)
return false;
return Pattern.compile("(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)){3}").matcher(str).matches();
}
/**
* 是否是身份证
*
* @param str
* @return
*/
public static boolean isIdentity(String str) {
if (isEmpty(str))
return false;
if (str.length() == 15)
return Pattern.compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$").matcher(str).matches();
if (str.length() == 18)
return Pattern.compile("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$").matcher(str).matches();
return false;
}