Android Interface some operations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.