To a certain person from Mars.
Includes: ①, get screen size
②, Hide EditText keyboard
③, open keyboard
④, turn off the keyboard
⑤, according to the phone's resolution from the DP unit to PX (pixels)
⑥, switch from px (pixel) units to DP based on the resolution of your phone
Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.text.InputType;ImportAndroid.util.DisplayMetrics;Importandroid.view.MotionEvent;ImportAndroid.view.inputmethod.InputMethodManager;ImportAndroid.widget.EditText;/*** @name Viewhelper * @description common system control operations, business-related *@authorMax * @date 2012-12-11 **/ Public classViewhelper {/*** Get screen size add by Max [2013-1-21] * *@paramactivity *@return */ Public Staticdisplaymetrics getdisplaymetrics (activity activity) {displaymetrics DM=NewDisplaymetrics (); Activity.getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); returnDM; } /*** Get screen size add by Max [2013-5-11] * *@paramContext *@return */ Public StaticDisplaymetrics Getdisplaymetrics (context context) {displaymetrics DM=context.getresources (). Getdisplaymetrics (); returnDM; } /*** Hide Editetext Keyboard {@code} setontouchlistener (New Ontouchlistener () {public * Boolean OnTouch (View V, motionevent event) {return true; * * @paramet * editetext*/ Public Static voidHiddenkeyboard (EditText et, motionevent event) {intIntype =Et.getinputtype (); Et.setinputtype (Inputtype.type_null); Et.ontouchevent (event); Et.setinputtype (Intype); Et.setselection (Et.gettext (). Length ()); } /*** Open Keyboard add by Max [2013-8-11]*/ Public Static voidOpenkeyboard (Finalcontext Context) {Inputmethodmanager IMM=(Inputmethodmanager) context. Getsystemservice (Context.input_method_service); Imm.togglesoftinput (0, inputmethodmanager.hide_not_always); } /*** Turn off keyboard add by Max [2013-7-2] *@paramContext *@paramEtinput*/ Public Static voidClosekeyboard (activity activity) {Inputmethodmanager InputManager=(Inputmethodmanager) activity. Getsystemservice (Context.input_method_service); Inputmanager.hidesoftinputfromwindow (Activity.getcurrentfocus (). Getwindowtoken (), Inputmethodmanager.hide_ Not_always); } /*** Switch from DP unit to PX (pixel) According to the resolution of the phone*/ Public Static intDIP2PX (Context context,floatdpvalue) { Final floatScale =context.getresources (). Getdisplaymetrics (). density; return(int) (Dpvalue * scale + 0.5f); } /*** According to the resolution of the phone from PX (pixel) to the unit to become DP*/ Public Static intPx2dip (Context context,floatpxvalue) { Final floatScale =context.getresources (). Getdisplaymetrics (). density; return(int) (Pxvalue/scale + 0.5f); }}
Android Interface some operations