Android仿苹果iphone数字锁屏解锁功能

跟着我一起按步骤来做,保证你一学就会。

步骤如下:

一、先自定义一个键盘布局文件:

在项目res/xml目录下新建一个xml文件,比如number_only.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:horizontalGap="0px"  
  4.     android:keyHeight="13%p"  
  5.     android:keyWidth="33%p"  
  6.     android:verticalGap="0px" >  
  7.   
  8.     <Row>  
  9.         <Key  
  10.             android:codes="49"  
  11.             android:keyLabel="1" />  
  12.         <Key  
  13.             android:codes="50"  
  14.             android:keyLabel="2" />  
  15.         <Key  
  16.             android:codes="51"  
  17.             android:keyLabel="3" />  
  18.     </Row>  
  19.     <Row>  
  20.         <Key  
  21.             android:codes="52"  
  22.             android:keyLabel="4" />  
  23.         <Key  
  24.             android:codes="53"  
  25.             android:keyLabel="5" />  
  26.         <Key  
  27.             android:codes="54"  
  28.             android:keyLabel="6" />  
  29.     </Row>  
  30.     <Row>  
  31.         <Key  
  32.             android:codes="55"  
  33.             android:keyLabel="7" />  
  34.         <Key  
  35.             android:codes="56"  
  36.             android:keyLabel="8" />  
  37.         <Key  
  38.             android:codes="57"  
  39.             android:keyLabel="9" />  
  40.     </Row>  
  41.     <Row>  
  42.         <Key  
  43.             android:codes="-2"  
  44.             android:keyLabel="" />  
  45.         <Key  
  46.             android:codes="48"  
  47.             android:keyLabel="0" />  
  48.         <Key  
  49.             android:codes="-5"  
  50.             android:keyIcon="@drawable/keyboard_delete" />  
  51.     </Row>  
  52.   
  53. </Keyboard>  

二、编写一个键盘事件处理的工具类,如KeyboardUtil.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.ArrayList;  
  2.   
  3. import android.app.Activity;  
  4. import android.inputmethodservice.Keyboard;  
  5. import android.inputmethodservice.KeyboardView;  
  6. import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;  
  7. import android.view.View;  
  8. import android.widget.EditText;  
  9.   
  10. public class KeyboardUtil {  
  11.     private Activity myActivity;  
  12.     private KeyboardView keyboardView;  
  13.     private Keyboard kb_num_only;  
  14.   
  15.     private ArrayList<EditText> listEd;  
  16.     private String thisPwdText = "";  
  17.   
  18.     public KeyboardUtil(Activity activity) {  
  19.         this.myActivity = activity;  
  20.         kb_num_only = new Keyboard(activity, R.xml.number_only);  
  21.         keyboardView = (KeyboardView) myActivity  
  22.                 .findViewById(R.id.keyboard_view);  
  23.         keyboardView.setKeyboard(kb_num_only);  
  24.         keyboardView.setEnabled(true);  
  25.         keyboardView.setPreviewEnabled(true);  
  26.         keyboardView.setOnKeyboardActionListener(listener);  
  27.     }  
  28.   
  29.     private OnKeyboardActionListener listener = new OnKeyboardActionListener() {  
  30.         @Override  
  31.         public void swipeUp() {  
  32.         }  
  33.   
  34.         @Override  
  35.         public void swipeRight() {  
  36.         }  
  37.   
  38.         @Override  
  39.         public void swipeLeft() {  
  40.         }  
  41.   
  42.         @Override  
  43.         public void swipeDown() {  
  44.         }  
  45.   
  46.         @Override  
  47.         public void onText(CharSequence text) {  
  48.         }  
  49.   
  50.         @Override  
  51.         public void onRelease(int primaryCode) {  
  52.         }  
  53.   
  54.         @Override  
  55.         public void onPress(int primaryCode) {  
  56.         }  
  57.   
  58.         @Override  
  59.         public void onKey(int primaryCode, int[] keyCodes) {  
  60.             if (primaryCode == -2) {  
  61.                 return;  
  62.             } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退  
  63.                 // 删除按钮所做的动作  
  64.                 if (thisPwdText != null && thisPwdText.length() >= 1) {  
  65.                     thisPwdText = thisPwdText.substring(0,  
  66.                             thisPwdText.length() - 1);  
  67.                     System.out.println("thisPwdText=" + thisPwdText);  
  68.                     int len = thisPwdText.length();  
  69.                     if (len <= 3) {  
  70.                         listEd.get(len).setText("");  
  71.                     }  
  72.                 }  
  73.             } else {  
  74.                 thisPwdText = thisPwdText + (char) primaryCode;  
  75.                 System.out.println("thisPwdText=" + thisPwdText);  
  76.                 int len = thisPwdText.length();  
  77.                 if (len <= 4) {  
  78.                     listEd.get(len - 1).setText("*");  
  79.                     if (len == 4) {  
  80.                         // 返回值,并清理本次记录,自动进入下次  
  81.                         listEd.get(4).setText(thisPwdText);  
  82.                         thisPwdText = "";  
  83.                     }  
  84.                 }  
  85.             }  
  86.         }  
  87.     };  
  88.   
  89.     /** 
  90.      * 包括四个密码输入框和一个密码保存框(按此顺序即可) 
  91.      *  
  92.      * @param etList 
  93.      */  
  94.     public void setListEditText(ArrayList<EditText> etList) {  
  95.         this.listEd = etList;  
  96.     }  
  97.   
  98.     // 显示键盘  
  99.     public void showKeyboard() {  
  100.         int visibility = keyboardView.getVisibility();  
  101.         if (visibility == View.GONE || visibility == View.INVISIBLE) {  
  102.             keyboardView.setVisibility(View.VISIBLE);  
  103.         }  
  104.     }  
  105.   
  106.     // 隐藏键盘  
  107.     public void hideKeyboard() {  
  108.         int visibility = keyboardView.getVisibility();  
  109.         if (visibility == View.VISIBLE) {  
  110.             keyboardView.setVisibility(View.INVISIBLE);  
  111.         }  
  112.     }  
  113. }  

三、新建一个activity窗口类:如SetLockPwdActivity.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.ArrayList;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.os.Message;  
  7. import android.text.Editable;  
  8. import android.text.InputType;  
  9. import android.text.TextWatcher;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.EditText;  
  13.   
  14. public class SetLockPwdActivity extends Activity {  
  15.     private View backView;  
  16.     private EditText etPwdOne, etPwdTwo, etPwdThree, etPwdFour, etPwdText;  
  17.     private KeyboardUtil kbUtil;  
  18.     public String strLockPwdOne;  
  19.     public String strLockPwdTwo;  
  20.     private Handler mHandler;  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         // TODO Auto-generated method stub  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_set_lock_pwd);  
  27.         findView();  
  28.         setListener();  
  29.         initData();  
  30.     }  
  31.   
  32.     void findView() {  
  33.         etPwdOne = (EditText) findViewById(R.id.etPwdOne_setLockPwd);  
  34.         etPwdTwo = (EditText) findViewById(R.id.etPwdTwo_setLockPwd);  
  35.         etPwdThree = (EditText) findViewById(R.id.etPwdThree_setLockPwd);  
  36.         etPwdFour = (EditText) findViewById(R.id.etPwdFour_setLockPwd);  
  37.         etPwdText = (EditText) findViewById(R.id.etPwdText_setLockPwd);  
  38.     }  
  39.   
  40.     void setListener() {  
  41.         etPwdText.addTextChangedListener(new TextWatcher() {  
  42.             @Override  
  43.             public void onTextChanged(CharSequence arg0, int arg1, int arg2,  
  44.                     int arg3) {  
  45.             }  
  46.   
  47.             @Override  
  48.             public void beforeTextChanged(CharSequence arg0, int arg1,  
  49.                     int arg2, int arg3) {  
  50.             }  
  51.   
  52.             @Override  
  53.             public void afterTextChanged(Editable arg0) {  
  54.                 if (etPwdFour.getText() != null  
  55.                         && etPwdFour.getText().toString().length() >= 1) {  
  56.                     new Thread(new Runnable() {  
  57.                         @Override  
  58.                         public void run() {  
  59.                             try {  
  60.                                 Thread.sleep(100);  
  61.                             } catch (Exception e) {  
  62.                                 e.printStackTrace();  
  63.                             } finally {  
  64.                                 Message msg = mHandler.obtainMessage();  
  65.                                 msg.what = R.id.doSuccess;  
  66.                                 mHandler.sendMessage(msg);  
  67.                             }  
  68.                         }  
  69.                     }).start();  
  70.                 }  
  71.             }  
  72.         });  
  73.     }  
  74.   
  75.     void initData() {  
  76.         kbUtil = new KeyboardUtil(SetLockPwdActivity.this);  
  77.         ArrayList<EditText> list = new ArrayList<EditText>();  
  78.         list.add(etPwdOne);  
  79.         list.add(etPwdTwo);  
  80.         list.add(etPwdThree);  
  81.         list.add(etPwdFour);  
  82.         list.add(etPwdText);  
  83.         kbUtil.setListEditText(list);  
  84.         etPwdOne.setInputType(InputType.TYPE_NULL);  
  85.         etPwdTwo.setInputType(InputType.TYPE_NULL);  
  86.         etPwdThree.setInputType(InputType.TYPE_NULL);  
  87.         etPwdFour.setInputType(InputType.TYPE_NULL);  
  88.         MyHandle();  
  89.     }  
  90.   
  91.     void backToActivity() {  
  92.         Intent mIntent = new Intent(SetLockPwdActivity.this, MainActivity.class);  
  93.         startActivity(mIntent);  
  94.     }  
  95.   
  96.   
  97.     public void MyHandle() {  
  98.         mHandler = new Handler() {  
  99.             @Override  
  100.             public void handleMessage(Message msg) {  
  101.                 super.handleMessage(msg);  
  102.                 switch (msg.what) {  
  103.                 case R.id.doSuccess:  
  104.                     if (etPwdFour.getText() != null  
  105.                             && etPwdFour.getText().toString().length() >= 1) {  
  106.                         if (strLockPwdOne != null  
  107.                                 && strLockPwdOne.length() == 4) {  
  108.                             String strReapt = etPwdText.getText().toString();  
  109.                             if (strReapt.equals(strLockPwdOne)) {  
  110.                                 Validate.Toast(SetLockPwdActivity.this,  
  111.                                         "解锁密码设置成功");  
  112.                                 strLockPwdOne = null;  
  113.                             } else {  
  114.                                 Validate.Toast(SetLockPwdActivity.this,  
  115.                                         "解锁密码设置失败");  
  116.                             }  
  117.                         } else {  
  118.                             strLockPwdOne = etPwdText.getText().toString();  
  119.                         }  
  120.                         etPwdOne.setText("");  
  121.                         etPwdTwo.setText("");  
  122.                         etPwdThree.setText("");  
  123.                         etPwdFour.setText("");  
  124.                     }  
  125.                     break;  
  126.                 default:  
  127.                     break;  
  128.                 }  
  129.             }  
  130.         };  
  131.     }  
  132.   
  133. }  

此activity对应的layout文件:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@color/black"  
  6.     android:orientation="vertical" >  
  7.   
  8.    
  9.   
  10.     <LinearLayout  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_gravity="center_horizontal"  
  14.         android:layout_marginTop="55dp"  
  15.         android:orientation="horizontal" >  
  16.   
  17.         <EditText  
  18.             android:id="@+id/etPwdOne_setLockPwd"  
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:background="@color/white"  
  22.             android:cursorVisible="false"  
  23.             android:ems="1"  
  24.             android:gravity="center"  
  25.             android:lines="1"  
  26.             android:textSize="31sp" >  
  27.         </EditText>  
  28.   
  29.         <EditText  
  30.             android:id="@+id/etPwdTwo_setLockPwd"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_marginLeft="16dp"  
  34.             android:background="@color/white"  
  35.             android:cursorVisible="false"  
  36.             android:ems="1"  
  37.             android:gravity="center"  
  38.             android:textSize="31sp" />  
  39.   
  40.         <EditText  
  41.             android:id="@+id/etPwdThree_setLockPwd"  
  42.             android:layout_width="wrap_content"  
  43.             android:layout_height="wrap_content"  
  44.             android:layout_marginLeft="16dp"  
  45.             android:background="@color/white"  
  46.             android:cursorVisible="false"  
  47.             android:ems="1"  
  48.             android:gravity="center"  
  49.             android:textSize="31sp" />  
  50.   
  51.         <EditText  
  52.             android:id="@+id/etPwdFour_setLockPwd"  
  53.             android:layout_width="wrap_content"  
  54.             android:layout_height="wrap_content"  
  55.             android:layout_marginLeft="16dp"  
  56.             android:background="@color/white"  
  57.             android:cursorVisible="false"  
  58.             android:ems="1"  
  59.             android:gravity="center"  
  60.             android:textSize="31sp" />  
  61.   
  62.         <EditText  
  63.             android:id="@+id/etPwdText_setLockPwd"  
  64.             android:layout_width="wrap_content"  
  65.             android:layout_height="wrap_content"  
  66.             android:visibility="gone" />  
  67.     </LinearLayout>  
  68.   
  69.     <LinearLayout  
  70.         android:layout_width="match_parent"  
  71.         android:layout_height="match_parent"  
  72.         android:gravity="center"  
  73.         android:orientation="vertical" >  
  74.   
  75.         <android.inputmethodservice.KeyboardView  
  76.             android:id="@+id/keyboard_view"  
  77.             android:layout_width="fill_parent"  
  78.             android:layout_height="wrap_content"  
  79.             android:layout_gravity="center"  
  80.             android:background="@color/lightblack"  
  81.             android:focusable="true"  
  82.             android:focusableInTouchMode="true"  
  83.             android:keyBackground="@drawable/keyboard_key"  
  84.             android:keyTextColor="@color/white" />  
  85.     </LinearLayout>  
  86.   
  87. </LinearLayout>  

四、最后一步,别忘了在AndroidManifest.xml配置文件中声明上面的activity类。


效果如下:

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值