天天看點

Android仿蘋果iphone數字鎖屏解鎖功能

跟着我一起按步驟來做,保證你一學就會。

步驟如下:

一、先自定義一個鍵盤布局檔案:

在項目res/xml目錄下建立一個xml檔案,比如number_only.xml

[html] view

plaincopy

Android仿蘋果iphone數字鎖屏解鎖功能
Android仿蘋果iphone數字鎖屏解鎖功能

<?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="49"  

            android:keylabel="1" />  

            android:codes="50"  

            android:keylabel="2" />  

            android:codes="51"  

            android:keylabel="3" />  

    </row>  

            android:codes="52"  

            android:keylabel="4" />  

            android:codes="53"  

            android:keylabel="5" />  

            android:codes="54"  

            android:keylabel="6" />  

            android:codes="55"  

            android:keylabel="7" />  

            android:codes="56"  

            android:keylabel="8" />  

            android:codes="57"  

            android:keylabel="9" />  

            android:codes="-2"  

            android:keylabel="" />  

            android:codes="48"  

            android:keylabel="0" />  

            android:codes="-5"  

            android:keyicon="@drawable/keyboard_delete" />  

</keyboard>  

二、編寫一個鍵盤事件處理的工具類,如keyboardutil.java

[java] view

Android仿蘋果iphone數字鎖屏解鎖功能
Android仿蘋果iphone數字鎖屏解鎖功能

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 = "";  

    public keyboardutil(activity activity) {  

        this.myactivity = activity;  

        kb_num_only = new keyboard(activity, 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() {  

        @override  

        public void swipeup() {  

        }  

        public void swiperight() {  

        public void swipeleft() {  

        public void swipedown() {  

        public void ontext(charsequence text) {  

        public void onrelease(int primarycode) {  

        public void onpress(int primarycode) {  

        public void onkey(int primarycode, int[] keycodes) {  

            if (primarycode == -2) {  

                return;  

            } else if (primarycode == keyboard.keycode_delete) {// 回退  

                // 删除按鈕所做的動作  

                if (thispwdtext != 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).settext("*");  

                    if (len == 4) {  

                        // 傳回值,并清理本次記錄,自動進入下次  

                        listed.get(4).settext(thispwdtext);  

                        thispwdtext = "";  

            }  

    };  

    /** 

     * 包括四個密碼輸入框和一個密碼儲存框(按此順序即可) 

     *  

     * @param etlist 

     */  

    public void setlistedittext(arraylist<edittext> etlist) {  

        this.listed = etlist;  

    // 顯示鍵盤  

    public void showkeyboard() {  

        int visibility = keyboardview.getvisibility();  

        if (visibility == view.gone || visibility == view.invisible) {  

            keyboardview.setvisibility(view.visible);  

    // 隐藏鍵盤  

    public void hidekeyboard() {  

        if (visibility == view.visible) {  

            keyboardview.setvisibility(view.invisible);  

}  

三、建立一個activity視窗類:如setlockpwdactivity.java

Android仿蘋果iphone數字鎖屏解鎖功能
Android仿蘋果iphone數字鎖屏解鎖功能

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.onclicklistener;  

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;  

    @override  

    protected void oncreate(bundle savedinstancestate) {  

        // todo auto-generated method stub  

        super.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() {  

            @override  

            public void ontextchanged(charsequence arg0, int arg1, int arg2,  

                    int arg3) {  

            public void beforetextchanged(charsequence arg0, int arg1,  

                    int arg2, int arg3) {  

            public void aftertextchanged(editable arg0) {  

                if (etpwdfour.gettext() != null  

                        && etpwdfour.gettext().tostring().length() >= 1) {  

                    new thread(new runnable() {  

                        @override  

                        public void run() {  

                            try {  

                                thread.sleep(100);  

                            } catch (exception e) {  

                                e.printstacktrace();  

                            } finally {  

                                message msg = mhandler.obtainmessage();  

                                msg.what = r.id.dosuccess;  

                                mhandler.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() {  

            public void handlemessage(message msg) {  

                super.handlemessage(msg);  

                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,  

                                        "解鎖密碼設定成功");  

                                strlockpwdone = null;  

                            } else {  

                                        "解鎖密碼設定失敗");  

                        } else {  

                            strlockpwdone = etpwdtext.gettext().tostring();  

                        etpwdone.settext("");  

                        etpwdtwo.settext("");  

                        etpwdthree.settext("");  

                        etpwdfour.settext("");  

                    break;  

                default:  

        };  

此activity對應的layout檔案:

Android仿蘋果iphone數字鎖屏解鎖功能
Android仿蘋果iphone數字鎖屏解鎖功能

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"  

    android:layout_width="match_parent"  

    android:layout_height="match_parent"  

    android:background="@color/black"  

    android:orientation="vertical" >  

    <linearlayout  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:layout_gravity="center_horizontal"  

        android:layout_margintop="55dp"  

        android:orientation="horizontal" >  

        <edittext  

            android:id="@+id/etpwdone_setlockpwd"  

            android:layout_width="wrap_content"  

            android:layout_height="wrap_content"  

            android:background="@color/white"  

            android:cursorvisible="false"  

            android:ems="1"  

            android:gravity="center"  

            android:lines="1"  

            android:textsize="31sp" >  

        </edittext>  

            android:id="@+id/etpwdtwo_setlockpwd"  

            android:layout_marginleft="16dp"  

            android:textsize="31sp" />  

            android:id="@+id/etpwdthree_setlockpwd"  

            android:id="@+id/etpwdfour_setlockpwd"  

            android:id="@+id/etpwdtext_setlockpwd"  

            android:visibility="gone" />  

    </linearlayout>  

        android:layout_width="match_parent"  

        android:layout_height="match_parent"  

        android:gravity="center"  

        android:orientation="vertical" >  

        <android.inputmethodservice.keyboardview  

            android:id="@+id/keyboard_view"  

            android:layout_width="fill_parent"  

            android:layout_gravity="center"  

            android:background="@color/lightblack"  

            android:focusable="true"  

            android:focusableintouchmode="true"  

            android:keybackground="@drawable/keyboard_key"  

            android:keytextcolor="@color/white" />  

</linearlayout>  

四、最後一步,别忘了在androidmanifest.xml配置檔案中聲明上面的activity類。

效果如下:

Android仿蘋果iphone數字鎖屏解鎖功能
Android仿蘋果iphone數字鎖屏解鎖功能
Android仿蘋果iphone數字鎖屏解鎖功能

繼續閱讀