Common functions and methods of the company's Android project, android Project
/*** Some common functions */public class FunctionUtil {private static long lastClickTime = 0;/*** is used at the beginning, and will not be limited later. Call the checkPhone method. Determine whether the mobile phone format is correct. When registering and modifying a mobile phone number, use ** @ param mobiles * @ return true: the correct mobile phone number */public static boolean isMobileNO (String mobiles) {// Pattern. compile ("^ (13 [0-9]) | (15 [^ 4, \ D]) | (18 [-9]) \ d {8} $ "); Pattern p = Pattern. compile ("^ (1 ([358] [0-9]) | (4 [57]) \ d {8} $ "); matcher m = p. matcher (mobiles); return m. matches ();}/*** verify the mobile phone number ** @ param context * @ param phone * @ return */public static boolean checkPhone (Cont Ext context, String phone) {if (null = phone | "". equals (phone) | "". equals (phone. trim () {FunctionUtil. toastMessage (context, "Enter the mobile phone number"); return false;} else if (phone. length ()! = 11 |! Phone. startsWith ("1") {FunctionUtil. toastMessage (context, "Incorrect Mobile Phone Number Format"); return false;} return true ;} /*** determines whether the user name contains special characters when illegal characters are registered ** @ return true: contains special characters, fasle does not contain */public static boolean containvalidchars (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;}/*** determines whether all numbers are used ** @ param str * @ return true: all numbers */public static boolean isNumer (String str) {Pattern pattern = Pattern. compile ("[0-9] *"); Matcher isNum = pattern. matcher (str); if (! IsNum. matches () {return false;} return true;}/*** determines whether the email format is correct ** @ 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 prompt statement public static void toastMessage (Context context, String message) {Toast. makeText (context, message, Toast. LENGTH_SHORT ). show ();}/*** used to print the information to the console for testing. During packaging, comment out the code in */public static void sysMessage (String message) {System. out. println (message);} public static String getTime () {Calendar c = Calendar. g EtInstance (); // get SimpleDateFormat s = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); return s. format (c. getTime (); // current date} public static String getTime2 () {Calendar Ar c = Calendar ar. getInstance (); // get SimpleDateFormat s = new SimpleDateFormat ("MM-dd HH: mm"); return s. format (c. getTime (); // current date}/*** format time, remove the second ** @ param time * @ return */public static String deleteSecond (String time) {if (ti Me = 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);}/*** format time, remove the year, second ** @ param time * @ return */public static String deleteNM (String time) {if (t Ime = 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);}/*** format time, hours, minutes, seconds ** @ 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);}/*** format time. Obtain the ** @ 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);}/*** format time, get the hour and minute ** @ 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);}/*** get the time to display ** @ param days * @ param diff * (in microseconds) * @ param hours * @ param minutes * points * @ param seconds * @ return */publi C 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 + "day" + hours + "Hour" + minutes + "Minute" + seconds + "second ";} else if (hours <= 0 & minutes> 0) {return minutes + "Minute" + seconds + "second ";} else if (hours <= 0 & minutes <= 0) {return seconds + "seconds ";} elsereturn hours + "Hour" + minutes + "Minute" + seconds + "second";} // capture the public static String captureString (String str) {if (strNotNull (str) & str. length ()> 4) {return str. substring (str. length ()-3 );} Else {return "" ;}}/*** convert from the unit of dp to px (pixel) */public static int dip2px (Context context, float dpValue) based on the cell phone resolution) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dpValue * scale + 0.5f);}/*** based on the resolution of the phone) to dp */public static int px2dip (Context context, float pxValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (in T) (pxValue/scale + 0.5f);} // generate a note of two-color ball (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. co Ntains (number) {number = (random. nextInt (33) + 1); // [1, 34)} else {list. add (number); array [I] = number; break ;}}list = null; Arrays. sort (array, 0, 6); // sort [0, 6) array [6] = (random. nextInt (16) + 1); // [) return array;} // generate a note of lotto data (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); // sort [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); // sort [5, 7) return array;} // generate a note fast three data (the three numbers are within the range of 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 )) {// Value 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 (numbe R) {number = (random. nextInt (6) + 1); // [1, 7)} else {list. add (number); array [I] = number; break ;}}} else if ("503 ". equals (playType) {// single answer 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) {// two different numbers 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); // [)} else {array [1] = number; break ;}} Arrays. sort (array); // sort return array;} // generate a note 11 to select 5 data (the five numbers are within the range of 1-11) public static int [] random11choose5 (String play_type) {int numberLenth = 8; if ("208 ". equals (play_type) {numberLenth = 8;} else if ("207 ". equals (p Lay_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 Rand Om (); 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); // sort return array;}/*** prevents users from clicking ** @ return continuously */Public static boolean isFastDoubleClick () {if (lastClickTime = 0) {// run lastClickTime = System for the first time at startup. currentTimeMillis (); return false;} // previously run in a local directory. Now, run long time = System in another Activity. currentTimeMillis (); long timeDistanse = time-lastClickTime; if (0 <timeDistanse & timeDistanse <800) {return true;} lastClickTime = time; return false ;} // judge whether the string is null. If it is not null, truepublic static Boolean strNotNull (Stri Ng str) {if (str = null | "null ". equals (str) | "". equals (str. trim () {return false;} return true;} // checks whether the list is empty. If not, returns truepublic static Boolean listNotNull (List <Object> list) {if (list = null | list. size () = 0) {return false;} return true;} // checks whether the list is empty. If not, returns truepublic static Boolean listStringNotNull (List <String> list) {if (list = null | list. size () = 0) {return false;} return true;} // judge ar Whether ray is null. if not null, truepublic static Boolean arrayNotNull (Object [] array) {if (array = null | array. length = 0) {return false;} return true;} // checks whether map is empty. If not, returns truepublic static Boolean mapNotNull (Map map) {if (map = null | map. size () = 0) {return false;} return true ;} /*** list sorting *** @ param code * @ return */public static ArrayList <String> sortArray (ArrayList <String> code) {// remove all 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 ;}}