Title This article shows some of the common tool class methods used in my development process and is not updated regularly.
Welcome to Daniel Criticism advice, if found wrong, welcome message advice, if there is a better way to achieve, also welcome message Exchange Study, thank you.
First, mobile phone number, the mailbox format matching tool class
Package com.kevin.test.utils;/** * String format matching tool class matches phone number, landline number, mailbox, etc. * * @author BLJ * */public class formatcheckutils{/** * determine if Compliant with the mailbox format */public static Boolean checkemailvalid (String stremail) {if (null = = Stremail) {return false;} return Stremail.matches ("[A-za-z0-9_][email protected][a-z0-9]+ (. [ a-z]+) {2} ");} /** * Determine if the format is in line with the phone number * * @param phonenumber * @return */public static Boolean checkphonenumbervalid (String phonenumber) {if ( NULL = = PhoneNumber) {return false;} /** * Matching Beijing and Shanghai 3-8 format: (^0[1,2]{1}\\d{1}-?\\d{8} * match other provinces, etc. 4-7/8 format: (^0[3-9]{1}\\d{2}-?\\d{7,8}) * Match internal phone transfer number: (-(\\d{1,4})) ? $) *///between the area code and the landline number you must add "-" String check = "((^0[1,2]{1}\\d{1}-?\\d{8}|) between the external number and the internal number. ^0[3-9]{1}\\d{2}-?\\d{7,8})) (-(\\d{1,4}))? $) "; return phonenumber.matches (check);} /** * Verify Phone number method * * @param strphonenum * @return */public static Boolean checkmobilenumbervalid (String strphonenum) {if (nul L = = Strphonenum) {return false;} /** * Match 13, 15, 18 start mobile phone number exclude 154 start phone number * Match 170, 176, 177, 178 start phone number * Match rule reference current (2015-04-29) Baidu Encyclopedia "mobile Number" list Number */stRing Checkphone = "^ ((13|18) [0-9]) | ( 15[^4,\\D] |170|176|177|178) \\d{8}$ "; return strphonenum.matches (Checkphone);}}
Second, the Android Toast tool class to make toast compare trouble extract encapsulates a value that passes only the context String or context stringid.
Import Android.content.context;import android.widget.toast;/** * Toast Tool class * * @author BLJ * */public class Toast utils{/** * Short tip by resId * * @param context * @param resId */public static void Shortshowresid (context context, int re SId) {Toast.maketext (context, resId, Toast.length_short). Show ();} /** * Long hint by resId * * @param context * @param resId */public static void Longshowresid (context context, int resId) {to Ast.maketext (context, resId, Toast.length_long). Show (); /** * Short hint by string * * @param context * @param string */public static void Shortshowstr (context context, string Strin g) {Toast.maketext (context, String, Toast.length_short). Show (); /** * often prompts by string * * @param context * @param string */public static void Longshowstr (context context, string string {Toast.maketext (context, String, Toast.length_long). Show ();}}
Third, Android cutting Paste tool class
Import Android.annotation.suppresslint;import Android.content.clipboardmanager;import android.content.Context; public class Clipboardutil {/** * implements text copy function * * @param content */@SuppressLint ("Newapi") public static void copy (Co ntext context, String content) {//Get clipboard manager Clipboardmanager CMB = (Clipboardmanager) context.getsystemservice ( Context.clipboard_service); Cmb.settext (Content.trim ());} /** * Implement paste function * * @param context * @return */@SuppressLint ("Newapi") public static String paste (context context) {// Get Clipboard manager Clipboardmanager CMB = (Clipboardmanager) context.getsystemservice (context.clipboard_service); return Cmb.gettext (). toString (). Trim ();}}
Four, Android DP, PX conversion Tool class
Import android.content.context;/** * DP and PX Conversion Tool * */public class densityutil{/** * Convert from Dip unit to PX (pixel) */public s According to the resolution of the phone tatic int dip2px (context context, float Dpvalue) {final float scale = context.getresources (). Getdisplaymetrics (). Density ; return (int) (Dpvalue * scale + 0.5f);} /** * According to the resolution of the phone from px (pixels) to the unit to a DP */public static int Px2dip (context context, float Pxvalue) {final float scale = CONTEXT.G Etresources (). Getdisplaymetrics (). Density;return (int) (Pxvalue/scale + 0.5f);} public static int Getpxfromstring (context context, String value) {String lowervalue = Value.tolowercase (); 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");}}}
Not to be continued, continuous update in ...
Welcome message criticism Advice, exchange study, thank you!
android| Common tool classes for Java development