Android Custom Numeric keypad

Source: Internet
Author: User

Business requirements Customize the numeric keypad and write a demo

Main code:

Import Android.app.activity;import Android.content.context;import Android.graphics.drawable.colordrawable;import Android.util.displaymetrics;import Android.view.view;import Android.view.windowmanager;import Android.view.viewgroup.layoutparams;import Android.widget.edittext;import Android.widget.imageview;import Android.widget.popupwindow;import Com.example.caculater.popupwindowutil.onpopuwindowlistener;import Com.example.caculaternumber.r;import Com.lidroid.xutils.viewutils;import Com.lidroid.xutils.view.annotation.viewinject;import Com.lidroid.xutils.view.annotation.event.onclick;public Class Caculaterpopuwindow{private static Activity mactivity;private static Caculaterpopuwindow mpopupwindow;private String phone= ""; @ViewInject (R.id.caculater_del) private ImageView Caculaterdel; @ViewInject (r.id.caculater_p) Private ImageView Caculaterp; @ViewInject (r.id.caculater_0) private ImageView Caculater0; @ViewInject (r.id.caculater_ 1) Private ImageView caculater1; @ViewInject (r.id.caculater_2) PrivateImageView caculater2; @ViewInject (r.id.caculater_3) private ImageView Caculater3; @ViewInject (r.id.caculater_4) Private ImageView caculater4; @ViewInject (r.id.caculater_5) private ImageView caculater5; @ViewInject (r.id.caculater_ 6) Private ImageView Caculater6; @ViewInject (r.id.caculater_7) private ImageView caculater7; @ViewInject ( r.id.caculater_8) Private ImageView Caculater8; @ViewInject (r.id.caculater_9) Private ImageView caculater9;@ Viewinject (r.id.edittext) Private editText edittext;private onpopuwindowlistener listener;private int mScreenWidth; private int Mscreenheight;public Caculaterpopuwindow () {}public static Caculaterpopuwindow getinstance (Activity Activity) {mactivity=activity;if (mpopupwindow==null) {mpopupwindow=new caculaterpopuwindow ();} return Mpopupwindow;} Public Popupwindow onCreate (Onpopuwindowlistener Listener) {//TODO auto-generated method Stubthis.listener=listener; View view=mactivity.getlayoutinflater (). Inflate (r.layout.include_caculater, NULL); Viewutils.inject (this, view); INITSCReen (); int HEIGHT=MSCREENHEIGHT/3; Popupwindow pw=new Popupwindow (view,layoutparams.match_parent,height);p w.setbackgrounddrawable (New ColorDrawable ( 0));p w.setoutsidetouchable (true); return PW;} @OnClick ({R.id.caculater_0,r.id.caculater_1,r.id.caculater_2,r.id.caculater_3,r.id.caculater_4,r.id.caculater_ 5,r.id.caculater_6,r.id.caculater_7,r.id.caculater_8,r.id.caculater_9,r.id.caculater_p,r.id.caculater_del}) private void Clickmethod (View v) {if (V==caculaterdel) {listener.delete ();} Else{if (V==CACULATER0) {phone= "0";} else if (v==caculater1) {phone= "1";} else if (v==caculater2) {phone= "2";} else if (v==caculater3) {phone= "3";} else if (v==caculater4) {phone= "4";} else if (V==CACULATER5) {phone= "5";} else if (V==CACULATER6) {phone= "6";} else if (v==caculater7) {phone= "7";} else if (V==caculater8) {phone= "8";} else if (V==caculater9) {phone= "9";} else if (v==caculaterp) {phone= ".";} Listener.addstring (phone);}} public void Initscreen () {WindowManager manager= (windowmanager) mactivity.getsystemservice (Context.window_service) ;D ISPLAymetrics outmetrics=new displaymetrics (); Manager.getdefaultdisplay (). Getmetrics (outmetrics); mScreenWidth= Outmetrics.widthpixels;mscreenheight=outmetrics.heightpixels;}


Import Android.text.inputtype;import Android.view.motionevent;import Android.view.view;import Android.widget.edittext;import Android.widget.popupwindow;public class Popupwindowutil implements Android.view.view.ontouchlistener{private static Popupwindowutil util;private EditText medittext;private PopupWindow Mpopuwindow;private Onpopuwindowlistener listener;public popupwindowutil () {}public static PopupWindowUtil GetInstance () {if (util==null) {util=new popupwindowutil ();} return util;} public void Setontouchlistener (EditText medittext,popupwindow Mpopuwindow,onpopuwindowlistener listener) { Medittext.setontouchlistener (this); this.medittext=medittext;this.mpopuwindow=mpopuwindow;this.listener= Listener;} @Overridepublic boolean OnTouch (View V, motionevent event) {//TODO auto-generated method Stubint Inputtype=medittext.get InputType (); Medittext.setinputtype (inputtype.type_null); Medittext.setinputtype (InputType); Medittext.setselection (Medittext.gettext (). toString (). Trim (). Length ()); if (mpopuwindow!=null&&!mpopuwindow.isshowing ()) {Listener.showpopuwindow ();} return false;} public interface onpopuwindowlistener{void Showpopuwindow (); void dismiss (); void addstring (String str); void Delete ();


Import Android.app.activity;import android.os.bundle;import Android.view.gravity;import Android.view.View;import Android.widget.edittext;import Android.widget.popupwindow;import Com.example.caculater.popupwindowutil.onpopuwindowlistener;import Com.lidroid.xutils.viewutils;public Abstract Class Baseactivity extends Activity implements onpopuwindowlistener{protected Popupwindow mpopupwindow;@ overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (Getcurrentview ()); Viewutils.inject (this); Initview ();} public abstract int Getcurrentview ();p ublic abstract void Initview ();p ublic void Initpopupwindow () {if (mpopupwindow== NULL) {mpopupwindow=caculaterpopuwindow.getinstance (this). OnCreate (This);}}            public void Startpopupwindow (view view) {if (mpopupwindow!=null&&!mpopupwindow.isshowing ()) {            Mpopupwindow.showatlocation (view, gravity.bottom, 0, 0); }}public void Stoppopupwindow ({if (mpopupwindow!=null&&mpopupwindow.isshowing ()) {Mpopupwindow.dismiss (); }}//add content to the text box public void addstring (EditText edittext,string sequence) {int index = geteditselection (EditText);// Position of the cursor if (Index < 0 | | Index >= getedittextviewstring (editText). Length ()) {edittext.append (sequence);} else { Edittext.geteditabletext (). Insert (index, sequence);//cursor position insert text}}//get cursor current position public int geteditselection (editText EditText) {return Edittext.getselectionstart ();} Gets the contents of the text box public String getedittextviewstring (EditText EditText) {return Edittext.gettext (). toString (); Clear the contents of the text box public void cleartext (EditText EditText) {Edittext.gettext (). Clear (); Deletes the character at the specified location public void Deleteeditvalue (EditText edittext,int index) {edittext.gettext (). Delete (index-1, index);} Set cursor position public void Seteditselectionloc (EditText edittext,int index) {edittext.setselection (index);} Determines whether the digital public static Boolean isnum (String str) {return str.matches ("([0-9]+)?) $");} public void ShowpopuWindow () {}public void Dismiss () {}public void addstring (String str) {}public void Delete () {}} 

Import Android.widget.edittext;import Android.widget.linearlayout;import com.example.caculater.BaseActivity; Import Com.example.caculater.popupwindowutil;import Com.lidroid.xutils.view.annotation.viewinject;public class Mainactivity extends Baseactivity {@ViewInject (r.id.edittext) private EditText editText; @ViewInject (r.id.parent) Private linearlayout parent; @Overridepublic int Getcurrentview () {//TODO auto-generated method Stubreturn R.layout.activity_main;} @Overridepublic void Initview () {//TODO auto-generated method Stubinitpopupwindow (); Popupwindowutil.getinstance (). Setontouchlistener (EditText, Mpopupwindow, this);} @Overridepublic void Showpopuwindow () {//TODO auto-generated method Stubsuper.showpopuwindow (); Startpopupwindow (parent);} @Overridepublic void Dismiss () {//TODO auto-generated method Stubsuper.dismiss (); Stoppopupwindow ();} @Overridepublic void Delete () {//TODO auto-generated method Stubsuper.delete (); if (Edittext.gettext (). toString (). Trim (). Length () >0) {Deleteeditvalue (EditText, Geteditselection (EditText));}} @Overridepublic void addstring (String str) {//TODO auto-generated method stubsuper.addstring (str); AddString (EditText, str);}}

This is the basic code, if you need a password keyboard (pure number), add a callback for the addstring parameter. Hide the system keyboard in the high version can not hide, and finally replaced a way of thinking, directly prohibit the system input method pop-up Inputtype.type_null, legacy issues: Cursor positioning Ontouchlistener When the cursor positioning everywhere to solve the position

Android Custom Numeric keypad

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.