公司Android項目公用通用的函數和方法,android項目
/** * 一些通用的函數 */public class FunctionUtil {private static long lastClickTime = 0;/** * 開始用的這種,後來就不限制,調用checkPhone方法。 判斷手機格式是否正確,在註冊和修改手機號碼的時候用到 * * @param mobiles * @return true:正確的手機號碼 */public static boolean isMobileNO(String mobiles) {// Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");Pattern p = Pattern.compile("^(1(([358][0-9])|(4[57])))\\d{8}$");Matcher m = p.matcher(mobiles);return m.matches();}/** * 校正手機號碼 * * @param context * @param phone * @return */public static boolean checkPhone(Context context, String phone) {if (null == phone || "".equals(phone) || "".equals(phone.trim())) {FunctionUtil.toastMessage(context, "請輸入手機號碼");return false;} else if (phone.length() != 11 || !phone.startsWith("1")) {FunctionUtil.toastMessage(context, "手機號碼格式不對");return false;}return true;}/** * 是否包含非法字元 註冊的時候判斷使用者名稱是否包含特殊字元 * * @return true :包含特殊字元,fasle就是不包含 */public static boolean containInvalidChars(String str) {if (str == null || "".equals(str))return false;String SPECIAL_STR = "#~!@%^&*();'\"?><[]{}\\|,:/=+—“”‘.`$;,。!@#¥%……&*()——+?";for (int i = 0; i < str.length(); i++) {if (SPECIAL_STR.indexOf(str.charAt(i)) != -1) {return true;}}return false;}/** * 判斷是否全是數字 * * @param str * @return true :全是數字 */public static boolean isNumer(String str) {Pattern pattern = Pattern.compile("[0-9]*");Matcher isNum = pattern.matcher(str);if (!isNum.matches()) {return false;}return true;}/** * 判斷email格式是否正確 * * @param email * @return */public static boolean isEmail(String email) {final String str_email = "^([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_email);Matcher m = p.matcher(email);return m.matches();}// Toast提示句public static void toastMessage(Context context, String message) {Toast.makeText(context, message, Toast.LENGTH_SHORT).show();}/** * 列印資訊到控制台 測試的時候用到,打包的時候注釋裡面的那句代碼 */public static void sysMessage(String message) {System.out.println(message);}public static String getTime() {Calendar c = Calendar.getInstance(); // 擷取東八區時間SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");return s.format(c.getTime()); // 當前日期}public static String getTime2() {Calendar c = Calendar.getInstance(); // 擷取東八區時間SimpleDateFormat s = new SimpleDateFormat("MM-dd HH:mm");return s.format(c.getTime()); // 當前日期}/** * 格式化時間,去掉秒 * * @param time * @return */public static String deleteSecond(String time) {if (time == null || "null".equals(time) || "".equals(time))return "";DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date dDate = null;try {dDate = format.parse(time);} catch (ParseException e) {e.printStackTrace();}format = new SimpleDateFormat("yyyy-MM-dd HH:mm");return format.format(dDate);}/** * 格式化時間,去掉年,秒 * * @param time * @return */public static String deleteNM(String time) {if (time == null || "null".equals(time) || "".equals(time))return "";DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date dDate = null;try {dDate = format.parse(time);} catch (ParseException e) {e.printStackTrace();}format = new SimpleDateFormat("MM-dd HH:mm");return format.format(dDate);}/** * 格式化時間,去小時,分鐘,秒 * * @param time * @return */public static String deleteHHMMSS(String time) {if (time == null || "null".equals(time) || "".equals(time))return "";DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date dDate = null;try {dDate = format.parse(time);} catch (ParseException e) {e.printStackTrace();}format = new SimpleDateFormat("yyyy-MM-dd");return format.format(dDate);}/** * 格式化時間,得到幾月幾日 * * @param time * @return */public static String getMonthAndDate(String time) {if (!strNotNull(time)) {return "";}DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date dDate = null;try {dDate = format.parse(time);} catch (ParseException e) {e.printStackTrace();}format = new SimpleDateFormat("MM-dd");return format.format(dDate);}/** * 格式化時間,擷取小時和分鐘 * * @param time * @return */public static String getHourAndMinute(String time) {if (!strNotNull(time)) {return "";}DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date dDate = null;try {dDate = format.parse(time);} catch (ParseException e) {e.printStackTrace();}format = new SimpleDateFormat("HH:mm");return format.format(dDate);}/** * 擷取要顯示的時間 * * @param days * 天 * @param diff * 到的的時間 (微秒) * @param hours * 時 * @param minutes * 分 * @param seconds * 秒 * @return */public static String getShowTime(long days, long diff, long hours, long minutes, long seconds) {days = diff / (1000 * 60 * 60 * 24);hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);seconds = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / (1000);if (days > 0) {return days + "天" + hours + "時" + minutes + "分" + seconds + "秒";} else if (hours <= 0 && minutes > 0) {return minutes + "分" + seconds + "秒";} else if (hours <= 0 && minutes <= 0) {return seconds + "秒";} elsereturn hours + "時" + minutes + "分" + seconds + "秒";}// 截取字串public static String captureString(String str) {if (strNotNull(str) && str.length() > 4) {return str.substring(str.length() - 3);} else {return "";}} /** * 根據手機的解析度從 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);}// 產生一注雙色球(6+1)public static int[] randomSSQ() {List<Integer> list = new ArrayList<Integer>();int[] array = new int[7];int number = -1;Random random = new Random();for (int i = 0; i < 6; i++) {number = (random.nextInt(33) + 1); // [1,34)// System.out.println("for--------------------");while (true) {// System.out.println("while--------------------" + number);if (list.contains(number)) {number = (random.nextInt(33) + 1); // [1,34)} else {list.add(number);array[i] = number;break;}}}list = null;Arrays.sort(array, 0, 6);// 排序 [0,6)array[6] = (random.nextInt(16) + 1); // [1,17)return array;}// 產生一注大樂透資料(5+2)public static int[] randomDLT() {List<Integer> list = new ArrayList<Integer>();int[] array = new int[7];int number = -1;Random random = new Random();for (int i = 0; i < 5; i++) {number = (random.nextInt(35) + 1); // [1,36)// System.out.println("for--------------------");while (true) {// System.out.println("while--------------------" + number);if (list.contains(number)) {number = (random.nextInt(35) + 1); // [1,36)} else {list.add(number);array[i] = number;break;}}}list = null;Arrays.sort(array, 0, 5);// 排序 [0,5)array[5] = (random.nextInt(12) + 1); // [1,13)number = (random.nextInt(12) + 1); // [1,13)while (number == array[5]) {number = (random.nextInt(12) + 1); // [1,13)}array[6] = number;Arrays.sort(array, 5, 7);// 排序 [5,7)return array;}// 產生一注快三資料(3個數字都在1-6範圍內)public static int[] randomK3(String playType) {List<Integer> list = new ArrayList<Integer>();int[] array = new int[3];int number = -1;Random random = new Random();if("501".equals(playType)){//和值for (int i = 0; i < 3; i++) {number = (random.nextInt(6) + 1); // [1,7)// System.out.println("for--------------------");array[i] = number;}}else if("506".equals(playType)){//三不同號for (int i = 0; i < 3; i++) {number = (random.nextInt(6) + 1); // [1,7)// System.out.println("for--------------------");while (true) {// System.out.println("while--------------------" + number);if (list.contains(number)) {number = (random.nextInt(6) + 1); // [1,7)} else {list.add(number);array[i] = number;break;}}}}else if("503".equals(playType)){//三同號單選number = (random.nextInt(6) + 1); // [1,7)for (int i = 0; i < 3; i++) {// System.out.println("for--------------------");array[i] = number;}}else if("507".equals(playType)){//二不同號array = new int[2];number = (random.nextInt(6) + 1); // [1,7)array[0] = number;list.add(number);while (true) {// System.out.println("while--------------------" + number);if (list.contains(number)) {number = (random.nextInt(6) + 1); // [1,7)} else {array[1] = number;break;}}}Arrays.sort(array);// 排序return array;}// 產生一注11選5資料(5個數字都在1-11範圍內)public static int[] random11choose5(String play_type) {int numberLenth = 8;if("208".equals(play_type)){numberLenth = 8;}else if("207".equals(play_type)){numberLenth = 7;}else if("206".equals(play_type)){numberLenth = 6;}else if("205".equals(play_type)){numberLenth = 5;}else if("204".equals(play_type)){numberLenth = 4;}else if("203".equals(play_type)){numberLenth = 3;}else if("202".equals(play_type)){numberLenth = 2;} List<Integer> list = new ArrayList<Integer>();int[] array = new int[numberLenth];int number = -1;Random random = new Random();for (int i = 0; i < numberLenth; i++) {number = (random.nextInt(11) + 1); // [1,12)// System.out.println("for--------------------");while (true) {// System.out.println("while--------------------" + number);if (list.contains(number)) {number = (random.nextInt(11) + 1); // [1,12)} else {list.add(number);array[i] = number;break;}}}Arrays.sort(array);// 排序return array;}/** * 防止使用者連續點擊 * * @return */public static boolean isFastDoubleClick() {if (lastClickTime == 0) {// 剛啟動第一次運行lastClickTime = System.currentTimeMillis();return false;}// 以前在地方運行過,現在在另外一個Activity運行long time = System.currentTimeMillis();long timeDistanse = time - lastClickTime;if (0 < timeDistanse && timeDistanse < 800) {return true;}lastClickTime = time;return false;} // 判斷字串是否為空白,不為空白返回truepublic static Boolean strNotNull(String str) {if (str == null || "null".equals(str) || "".equals(str.trim())) {return false;}return true;}// 判斷list是否為空白,不為空白返回truepublic static Boolean listNotNull(List<Object> list) {if (list == null || list.size() == 0) {return false;}return true;}// 判斷list是否為空白,不為空白返回truepublic static Boolean listStringNotNull(List<String> list) {if (list == null || list.size() == 0) {return false;}return true;}// 判斷array是否為空白,不為空白返回truepublic static Boolean arrayNotNull(Object[] array) {if (array == null || array.length == 0) {return false;}return true;}// 判斷map是否為空白,不為空白返回truepublic static Boolean mapNotNull(Map map) {if (map == null || map.size() == 0) {return false;}return true;} /** * list排序 * * @param code * @return */public static ArrayList<String> sortArray(ArrayList<String> code) {// 移除所有不是數位for (int i = 0; i < code.size(); i++)code.set(i, code.get(i) == null ? "0" : code.get(i).replaceAll("//D+", ""));for (int i = 0; i < code.size(); i++) {for (int j = i; j < code.size(); j++) {if (Integer.valueOf(code.get(i)) > Integer.valueOf(code.get(j))) {String tmp = code.get(j);code.set(j, code.get(i));code.set(i, tmp);}}}return code;} }