android--Imitation Apple digital unlocking function

Source: Internet
Author: User

Follow me along the steps to make sure you learn.

The steps are as follows:

First, customize a keyboard layout file:

Create a new XML file under the project Res/xml directory, such as Number_only.xml

<?xml version= "1.0" encoding= "Utf-8"? ><keyboard xmlns:android= "http://schemas.android.com/apk/res/ Android "android:horizontalgap=" 0px "android:keyheight=" 13%p "android:keywidth=" 33%p "android:verticalgap=" 0px            "> <Row> <key android:codes=" android:keylabel= "1"/> <key            Android:codes= "android:keylabel=" 2 "/> <key android:codes=" 51 " Android:keylabel= "3"/> </Row> <Row> <key android:codes= "*" Android:            Keylabel= "4"/> <key android:codes= "5" android:keylabel=/> Android:codes= "android:keylabel=" 6 "/> </Row> <Row> <key an Droid:codes= "android:keylabel=" 7 "/> <key android:codes=" Android:key "         Label= "8"/> <key   Android:codes= "android:keylabel=" 9 "/> </Row> <Row> <key Andro Id:codes= "-2" android:keylabel= ""/> <key android:codes= "Android:keylabe" l= "0"/> <key android:codes= "-5" android:keyicon= "@drawable/keyboard_delete"/> & Lt;/row></keyboard>

Ii. Write a tool class for keyboard event handling, such as Keyboardutil.java

Import Java.util.arraylist;import Android.app.activity;import Android.inputmethodservice.keyboard;import Android.inputmethodservice.keyboardview;import Android.inputmethodservice.KeyboardView.OnKeyboardActionListener Import Android.view.view;import Android.widget.edittext;public class Keyboardutil {private Activity myactivity; Private Keyboardview keyboardview;private Keyboard kb_num_only;private arraylist<edittext> listEd;private String Thispwdtext = "";p ublic keyboardutil (activity activity) {this.myactivity = Activity;kb_num_only = new Keyboard (act Ivity, r.xml.number_only); Keyboardview = (Keyboardview) Myactivity.findviewbyid (R.id.keyboard_view); Keyboardview.setkeyboard (kb_num_only); keyboardview.setenabled (true); keyboardview.setpreviewenabled (true); Keyboardview.setonkeyboardactionlistener (listener);} Private Onkeyboardactionlistener listener = new Onkeyboardactionlistener () {@Overridepublic void Swipeup () {}@ overridepublic void Swiperight () {} @Overridepublic void Swipeleft () {}@overridepublic void Swipedown () {} @Overridepublic void Ontext (charsequence text) {} @Overridepublic void onrelease (int Primarycode) {} @Overridepublic void onpress (int primarycode) {} @Overridepublic void OnKey (int primarycode, int[] Keycodes) {if (Primarycode = =-2) {return;} else if (Primarycode = = Keyboard.keycode_delete) {//Fallback/Delete button action if (thispwd Text! = null && thispwdtext.length () >= 1) {thispwdtext = Thispwdtext.substring (0,thispwdtext.length ()-1); System.out.println ("thispwdtext=" + thispwdtext); int len = Thispwdtext.length (); if (len <= 3) {listed.get (len). SetText ("");}} else {Thispwdtext = Thispwdtext + (char) Primarycode; System.out.println ("thispwdtext=" + thispwdtext); int len = Thispwdtext.length (); if (len <= 4) {listed.get (len-1). Set Text ("*"), if (len = = 4) {//return value, and clean this record, automatically enter the next Listed.get (4). SetText (thispwdtext); thispwdtext = "";}}}}};/ * * includes four password entry boxes and a password save box (in this order) * * @param etlist */public void Setlistedittext (arraylist<edittext> etlist) {this.listed = etlist;}  Show keyboard public void Showkeyboard () {int visibility = keyboardview.getvisibility (); if (visibility = = View.gone | | visibility = = view.invisible) {keyboardview.setvisibility (view.visible);}} Hide keyboard public void Hidekeyboard () {int visibility = keyboardview.getvisibility (); if (visibility = = view.visible) {Keyboar Dview.setvisibility (view.invisible);}}}

Third, create a new Activity window class: such as Setlockpwdactivity.java

Import Java.util.arraylist;import android.app.activity;import android.content.intent;import android.os.Bundle; Import Android.os.handler;import android.os.message;import android.text.editable;import android.text.InputType; Import Android.text.textwatcher;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.edittext;public class Setlockpwdactivity extends Activity {private View backview;private EditText Etpwdone, Etpwdtwo, Etpwdthree, Etpwdfour, etpwdtext;private keyboardutil kbutil;public String strLockPwdOne;public String strlockpwdtwo;private Handler mhandler; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO Auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_SET_LOCK_PWD); Findview (); Setlistener (); InitData ();} void Findview () {Etpwdone = (EditText) Findviewbyid (r.id.etpwdone_setlockpwd); etpwdtwo = (EditText) Findviewbyid ( R.ID.ETPWDTWO_SETLOCKPWD); etpwdthree = (EditText) Findviewbyid (R.ID.ETPWDTHREE_SETLOCKPWD); etpwdfour = (EditText) Findviewbyid (r.id.etpwdfour_setlockpwd); etpwdtext = (EditText) Findviewbyid (R.ID.ETPWDTEXT_SETLOCKPWD);} void Setlistener () {Etpwdtext.addtextchangedlistener (new Textwatcher () {@Overridepublic void ontextchanged ( Charsequence arg0, int arg1, int arg2,int arg3) {} @Overridepublic void beforetextchanged (charsequence arg0, int arg1,int A RG2, int arg3) {} @Overridepublic void aftertextchanged (Editable arg0) {if (Etpwdfour.gettext ()! = null&& Etpwdfou  R.gettext (). toString (). Length () >= 1) {New Thread (new Runnable () {@Overridepublic void run () {try {thread.sleep (100)} catch (Exception e) {e.printstacktrace ();} finally {Message msg = Mhandler.obtainmessage (); msg.what = R.id.dosuccess;mha Ndler.sendmessage (msg);}}). Start ();}});} void InitData () {kbutil = new Keyboardutil (setlockpwdactivity.this); arraylist<edittext> list = new arraylist<edittext> (); List.add (Etpwdone); List.add (etpwdtwo); List.add ( Etpwdthree); List.add (etpwdfour); list.add(Etpwdtext); Kbutil.setlistedittext (list); Etpwdone.setinputtype (inputtype.type_null); Etpwdtwo.setinputtype ( Inputtype.type_null); Etpwdthree.setinputtype (inputtype.type_null); Etpwdfour.setinputtype (InputType.TYPE_NULL); Myhandle ();} void Backtoactivity () {Intent mintent = new Intent (setlockpwdactivity.this, Mainactivity.class); StartActivity (mIntent );} public void Myhandle () {Mhandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {Super.handlemessage (ms g); switch (msg.what) {case r.id.dosuccess:if (etpwdfour.gettext ()! = null&& Etpwdfour.gettext (). toString (). Length () >= 1) {if (Strlockpwdone! = null&& strlockpwdone.length () = = 4) {String strreapt = Etpwdtext.gettext () . toString (); if (Strreapt.equals (Strlockpwdone)) {Validate.toast (setlockpwdactivity.this, "Unlock password setting succeeded"); Strlockpwdone = null;} else {validate.toast (setlockpwdactivity.this, "Unlock password settings failed");}} else {strlockpwdone = Etpwdtext.gettext (). toString ();} Etpwdone.settext (""); Etpwdtwo.settext (""); Etpwdthree.settExt (""); Etpwdfour.settext ("");} Break;default:break;}}};}}

This activity corresponds to the layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:background=" @color/blac K "android:orientation=" vertical "> <linearlayout android:layout_width=" wrap_content "android:l        ayout_height= "Wrap_content" android:layout_gravity= "Center_horizontal" android:layout_margintop= "55DP" android:orientation= "Horizontal" > <edittext android:id= "@+id/etpwdone_setlockpwd" an Droid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:background= "@color/WH Ite "android:cursorvisible=" false "android:ems=" 1 "android:gravity=" center "an droid:lines= "1" android:textsize= "31SP" > </EditText> <edittext android:id        = "@+id/etpwdtwo_setlockpwd"    Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_marginle             ft= "16DP" android:background= "@color/white" android:cursorvisible= "false" android:ems= "1" android:gravity= "center" android:textsize= "31sp"/> <edittext android:id= "@            +id/etpwdthree_setlockpwd "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:layout_marginleft= "16DP" android:background= "@color/white" android:cursorvisible= "False" android:ems= "1" android:gravity= "center" android:textsize= "31SP"/> &lt ; EditText android:id= "@+id/etpwdfour_setlockpwd" android:layout_width= "Wrap_content" Andro            id:layout_height= "Wrap_content" android:layout_marginleft= "16DP" android:background= "@color/white" Android:cursoRvisible= "false" android:ems= "1" android:gravity= "center" android:textsize= "31SP"/>            <edittext android:id= "@+id/etpwdtext_setlockpwd" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:visibility= "Gone"/> </LinearLayout> <linea Rlayout android:layout_width= "match_parent" android:layout_height= "match_parent" android:gravity= "cen ter "android:orientation=" vertical "> <android.inputmethodservice.keyboardview android:id="            @+id/keyboard_view "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "            android:layout_gravity= "center" android:background= "@color/lightblack" android:focusable= "true" Android:focusableintouchmode= "true" android:keybackground= "@drawable/keyboard_key" Android : keytextcolor= "@color/WHIte "/> </LinearLayout></LinearLayout> 

Four, the last step, don't forget to declare the activity class above in the Androidmanifest.xml configuration file.


The effect is as follows:



android--Imitation Apple digital unlocking function

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.