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

步骤如下:

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

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

[html] view plaincopy
  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. <Row>
  8. <Key
  9. android:codes="49"
  10. android:keyLabel="1" />
  11. <Key
  12. android:codes="50"
  13. android:keyLabel="2" />
  14. <Key
  15. android:codes="51"
  16. android:keyLabel="3" />
  17. </Row>
  18. <Row>
  19. <Key
  20. android:codes="52"
  21. android:keyLabel="4" />
  22. <Key
  23. android:codes="53"
  24. android:keyLabel="5" />
  25. <Key
  26. android:codes="54"
  27. android:keyLabel="6" />
  28. </Row>
  29. <Row>
  30. <Key
  31. android:codes="55"
  32. android:keyLabel="7" />
  33. <Key
  34. android:codes="56"
  35. android:keyLabel="8" />
  36. <Key
  37. android:codes="57"
  38. android:keyLabel="9" />
  39. </Row>
  40. <Row>
  41. <Key
  42. android:codes="-2"
  43. android:keyLabel="" />
  44. <Key
  45. android:codes="48"
  46. android:keyLabel="0" />
  47. <Key
  48. android:codes="-5"
  49. android:keyIcon="@drawable/keyboard_delete" />
  50. </Row>
  51. </Keyboard>

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

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

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

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

此activity对应的layout文件:

[html] view plaincopy
  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. <LinearLayout
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center_horizontal"
  11. android:layout_marginTop="55dp"
  12. android:orientation="horizontal" >
  13. <EditText
  14. android:id="@+id/etPwdOne_setLockPwd"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:background="@color/white"
  18. android:cursorVisible="false"
  19. android:ems="1"
  20. android:gravity="center"
  21. android:lines="1"
  22. android:textSize="31sp" >
  23. </EditText>
  24. <EditText
  25. android:id="@+id/etPwdTwo_setLockPwd"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_marginLeft="16dp"
  29. android:background="@color/white"
  30. android:cursorVisible="false"
  31. android:ems="1"
  32. android:gravity="center"
  33. android:textSize="31sp" />
  34. <EditText
  35. android:id="@+id/etPwdThree_setLockPwd"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_marginLeft="16dp"
  39. android:background="@color/white"
  40. android:cursorVisible="false"
  41. android:ems="1"
  42. android:gravity="center"
  43. android:textSize="31sp" />
  44. <EditText
  45. android:id="@+id/etPwdFour_setLockPwd"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:layout_marginLeft="16dp"
  49. android:background="@color/white"
  50. android:cursorVisible="false"
  51. android:ems="1"
  52. android:gravity="center"
  53. android:textSize="31sp" />
  54. <EditText
  55. android:id="@+id/etPwdText_setLockPwd"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:visibility="gone" />
  59. </LinearLayout>
  60. <LinearLayout
  61. android:layout_width="match_parent"
  62. android:layout_height="match_parent"
  63. android:gravity="center"
  64. android:orientation="vertical" >
  65. <android.inputmethodservice.KeyboardView
  66. android:id="@+id/keyboard_view"
  67. android:layout_width="fill_parent"
  68. android:layout_height="wrap_content"
  69. android:layout_gravity="center"
  70. android:background="@color/lightblack"
  71. android:focusable="true"
  72. android:focusableInTouchMode="true"
  73. android:keyBackground="@drawable/keyboard_key"
  74. android:keyTextColor="@color/white" />
  75. </LinearLayout>
  76. </LinearLayout>

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

效果如下:

Android仿苹果iphone数字锁屏解锁功能相关推荐

  1. 一键清除苹果锁屏密码_忘记苹果iPhone/iPad锁屏密码怎么办?外媒支招

    9月16日消息 虽然苹果已开发出Face ID和Touch ID等生物识别验证技术,目前已很少有人通过密码解锁自己的iPhone或iPad了.不过密码解锁设备仍然是一种极为重要的解锁方式,而用户在不慎 ...

  2. android解锁界面分析,Android 7.0 锁屏解锁之向上滑动显示解锁界面分析

    Android 7.0 锁屏解锁之向上滑动显示解锁界面分析 by jing.chen 锁屏的解锁操作是在锁屏界面向上滑动实现的,通过向上滑动调出解锁界面(如图案.PIN.密码解锁界面),在解锁界面输入 ...

  3. iphone手机锁屏密码怎么解锁?苹果手机如何解除屏幕锁

    iphone手机锁屏密码怎么解锁?你输入正确的密码并解锁手机是正常的,但在这种情况下,如果你输入错误的密码超过10次,那么iPhone将被禁用,并弹出一个消息 "iPhone被禁用,连接到i ...

  4. 高仿IPhone滑动锁屏

    下面的网址是我在eoe上面发表的帖子,源代码在里面,可以去下载. http://www.eoeandroid.com/thread-239655-1-1.html 最近公司在锁屏功能,类似小米的百变锁 ...

  5. android仿iphone页面,Android仿苹果关机界面实现代码

    本文实例为大家分享了Android仿苹果关机界面的具体代码,供大家参考,具体内容如下 主class 用来控制viewdialog的显示 package com.android.server.polic ...

  6. iPhone忘记锁屏密码,多次输错被禁用?三种方法轻松解决!

    为了保证手机的隐私安全,我们通常会将手机设置锁屏密码.最近有许多果粉反馈,自己长期不用的iPhone 忘记锁屏密码打不开了,多次输错密码还被禁用了,怎么办? 今天,小编就针对苹果手机忘记锁屏密码这个问 ...

  7. Android安全学习笔记1——锁屏密码方式

    前言 在Android安全学习中,我接触到第一个例子是锁屏密码.我们日常使用手机的时候使用最多的锁屏密码是怎么构成的?下面分享一下我接触到的知识. 锁屏密码的思考 为了安全,Android设备在解锁屏 ...

  8. iphonex重量_精仿苹果iPhone X手机配置介绍

    精仿苹果iPhone X手机配置介绍 [上市时间] 2017年10月最新版 [屏幕色彩] 1600万 [分 辨 率] 1920X1080 [屏幕尺寸] 5.8英寸IPS全视角电容式触摸屏 [处 理 器 ...

  9. android 监听屏幕是否锁屏

    今天,简单讲讲如何监听手机屏幕是否锁屏. 实现方法:1)通过BroadcastReceiver接收广播Intent.ACTION_SCREEN_ON和Intent.ACTION_SCREEN_OFF可 ...

最新文章

  1. js两个小技巧【看到了就记录一下】
  2. Vue工程模板文件 webpack打包
  3. Eclipse上安装maven插件时出错
  4. 格雷码Gray Code详解
  5. Atitit 安全规范 指南 常见五种意外防止规范 attilax总结
  6. Python:第六次全国人口普查数据分析及可视化(pandas、matplotlib)
  7. mxm智能教育机器人无法智能对话_关于智能语音机器人使用中可能出现的问题
  8. EasyUI仓库管理系统
  9. 学习 Java全栈工程师6.0 初学者笔记1 2021-08-09
  10. 中英文电子书免费下载网站大全
  11. Python学习_案例for循环嵌套
  12. ES6 里的symbol
  13. Datawhale打卡活动 Kaggle Spaceship Titanic Day3
  14. ReentrantReadWriteLock、StampedLock读写锁
  15. CIA3 NOI接站(tarjan缩环+Floyd传递闭包+可相交最小路径覆盖)
  16. LINQ之Update
  17. 关于组织2021-2022全国青少年电子信息 智能创新大赛西北赛区(陕西)复赛的通知
  18. 需要计算机安装msxml,Win7安装Office2010提示让安装MSXML组件的五种解决方法
  19. 12槽10极分数槽集中绕组永磁电机结构讲解
  20. 录屏鼠标光标圆圈如何实现_自定义录制时鼠标形状

热门文章

  1. 全国高校人工智能与大数据创新联盟,首推云创高校大数据与人工智能实验室建设方案...
  2. 中国大学MOOC课程《Python语言程序设计》第五章 七段数码管绘制 引发的时间问题和海龟速度问题
  3. ROCKET PROPULSION ELEMENTS——CLASSIFICATION笔记
  4. C#:实现九九乘法表算法(附完整源码)
  5. java的gettime,java.util.Calendar.getTime()方法实例
  6. UGUI-- Scrollview 滚动视图
  7. 为什么程序员应该从现在就开始看书
  8. 用函数编写十进制转化为二进制
  9. 数字营销闭环解决方案
  10. Python爬虫深入 爬取当当网商品基本信息