Android | common tools for Java Development
For example, this article shows some of the frequently-used tool methods used in the development process, which are occasionally updated.
Thank you for your criticism and advice. If any errors are found, please leave a message. If there is a better implementation method, please leave a message for us to learn. Thank you.
Package com. kevin. test. utils; /*** string format matching tool-type Matching: mobile phone number, landline number, mailbox, etc. ** @ author blj **/public class FormatCheckUtils {/*** determine whether the email format is correct */public static boolean checkEmailValid (String strEmail) {if (null = strEmail) {return false;} return strEmail. matches ([a-zA-Z0-9 _] + @ [a-z0-9] + (. [a-z] +) {2 });} /*** determine whether the landline number format is correct ** @ param phoneNumber * @ return */public static boolean checkPhoneNumberValid (String phoneNumber) {if (Null = phoneNumber) {return false;}/*** matches 3-8 formats such as Beijing and Shanghai: (^ 0 [1, 2] {1} \ d {1 }-? \ D {8} * matches 4-7/8 formats such as other provinces: (^ 0 [3-9] {1} \ d {2 }-? \ D {}) * matches the internal call transfer number (-(\ d }))? $) * // Do not add "-" between the area code and landline number. You must add "-" String check = (^ 0 [] {1} \ d) between the external number and the internal number. {1 }-? \ D {8} | (^ 0 [3-9] {1} \ d {2 }-? \ D {}) (-(\ d }))? $); Return phoneNumber. matches (check);}/*** mobile phone number verification method ** @ param strPhoneNum * @ return */public static boolean checkMobileNumberValid (String strPhoneNum) {if (null = strPhoneNum) {return false;}/*** match a mobile phone number starting with 13, 15, and 18. exclude a mobile phone number starting with 154 * match a mobile phone number starting with 170, 176, 177, and 178 *. Refer to the current rule) baidu Encyclopedia "mobile phone number" listing Number */String checkphone = ^ (13 | 18) [0-9]) | (15 [^ 4, \ D]) | 170 | 176 | 177 | 178) \ d {8 }$; return strPhoneNum. matches (checkphone );}}
Ii. Playing Toast in the Android Toast tool class makes it easier to extract and encapsulate the following information: Pass the value only to Context String or Context StringID.
Import android. content. context; import android. widget. toast; /*** Toast tool class ** @ author blj **/public class ToastUtils {/*** short message by resId ** @ param context * @ param resId */public static void using showresid (Context context, int resId) {Toast. makeText (context, resId, Toast. LENGTH_SHORT ). show ();}/*** long prompt by resId ** @ param context * @ param resId */public static void longShowResId (Context context, int resId) {Toast. makeText (context, resId, Toast. LENGTH_LONG ). show ();}/*** short message by String ** @ param context * @ param string */public static void merge showstr (Context context, String string) {Toast. makeText (context, string, Toast. LENGTH_SHORT ). show ();}/*** is often prompted by String ** @ param context * @ param string */public static void longShowStr (Context context, String string) {Toast. makeText (context, string, Toast. LENGTH_LONG ). show ();}}
Iii. Android cutting and pasting Tool
Import android. annotation. suppressLint; import android. content. clipboardManager; import android. content. context; public class ClipBoardUtil {/*** implements the text copy function ** @ param content */@ SuppressLint (NewApi) public static void copy (Context context, String content) {// obtain the clipboard manager ClipboardManager cmb = (ClipboardManager) context. getSystemService (Context. CLIPBOARD_SERVICE); cmb. setText (content. trim ();}/*** paste function ** @ param context * @ return */@ SuppressLint (NewApi) public static String paste (Context context) {// obtain the clipboard manager ClipboardManager cmb = (ClipboardManager) context. getSystemService (Context. CLIPBOARD_SERVICE); return cmb. getText (). toString (). trim ();}}
Iv. Android dp and px conversion tools
Import android. content. context;/*** dp and px Conversion Tool **/public class DensityUtil {/*** convert the unit from dip to pixel according to the resolution of the mobile phone) */public static int dip2px (Context context, float dpValue) {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 (int) (pxValue/scale + 0.5f);} public static int getPXFromString (Context context, String value) {String lowerValue = value. toLowerCase (); if (lowerValue. endsWith (px) {return Integer. parseInt (lowerValue. substring (0, lowerValue. indexOf (px);} else if (lowerValue. endsWith (dp) | lowerValue. endsWith (dip) {return dip2px (context, Integer. parseInt (lowerValue. substring (0, lowerValue. indexOf (d);} else if (lowerValue. matches (\ d +) {return Integer. parseInt (lowerValue);} else {throw new RuntimeException (invalid conversion string );}}}