Android仿苹果iphone数字锁屏解锁功能
跟着我一起按步骤来做,保证你一学就会。
步骤如下:
一、先自定义一个键盘布局文件:
在项目res/xml目录下新建一个xml文件,比如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="49"
- android:keyLabel="1" />
- <Key
- android:codes="50"
- android:keyLabel="2" />
- <Key
- android:codes="51"
- android:keyLabel="3" />
- </Row>
- <Row>
- <Key
- android:codes="52"
- android:keyLabel="4" />
- <Key
- android:codes="53"
- android:keyLabel="5" />
- <Key
- android:codes="54"
- android:keyLabel="6" />
- </Row>
- <Row>
- <Key
- android:codes="55"
- android:keyLabel="7" />
- <Key
- android:codes="56"
- android:keyLabel="8" />
- <Key
- android:codes="57"
- android:keyLabel="9" />
- </Row>
- <Row>
- <Key
- android:codes="-2"
- android:keyLabel="" />
- <Key
- android:codes="48"
- android:keyLabel="0" />
- <Key
- android:codes="-5"
- android:keyIcon="@drawable/keyboard_delete" />
- </Row>
- </Keyboard>
二、编写一个键盘事件处理的工具类,如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 = "";
- 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() {
- }
- @Override
- public void swipeRight() {
- }
- @Override
- public void swipeLeft() {
- }
- @Override
- public void swipeDown() {
- }
- @Override
- public void onText(CharSequence text) {
- }
- @Override
- public void onRelease(int primaryCode) {
- }
- @Override
- public void onPress(int primaryCode) {
- }
- @Override
- 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() {
- int visibility = keyboardView.getVisibility();
- if (visibility == View.VISIBLE) {
- keyboardView.setVisibility(View.INVISIBLE);
- }
- }
- }
三、新建一个activity窗口类:如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;
- @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) {
- }
- @Override
- public void beforeTextChanged(CharSequence arg0, int arg1,
- int arg2, int arg3) {
- }
- @Override
- 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() {
- @Override
- 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 {
- Validate.Toast(SetLockPwdActivity.this,
- "解锁密码设置失败");
- }
- } else {
- strLockPwdOne = etPwdText.getText().toString();
- }
- etPwdOne.setText("");
- etPwdTwo.setText("");
- etPwdThree.setText("");
- etPwdFour.setText("");
- }
- break;
- default:
- break;
- }
- }
- };
- }
- }
此activity对应的layout文件:
- <?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/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>
- <EditText
- android:id="@+id/etPwdTwo_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" />
- <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" />
- <EditText
- android:id="@+id/etPwdFour_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" />
- <EditText
- android:id="@+id/etPwdText_setLockPwd"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:visibility="gone" />
- </LinearLayout>
- <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_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>
四、最后一步,别忘了在AndroidManifest.xml配置文件中声明上面的activity类。
效果如下:
Android仿苹果iphone数字锁屏解锁功能相关推荐
- 一键清除苹果锁屏密码_忘记苹果iPhone/iPad锁屏密码怎么办?外媒支招
9月16日消息 虽然苹果已开发出Face ID和Touch ID等生物识别验证技术,目前已很少有人通过密码解锁自己的iPhone或iPad了.不过密码解锁设备仍然是一种极为重要的解锁方式,而用户在不慎 ...
- android解锁界面分析,Android 7.0 锁屏解锁之向上滑动显示解锁界面分析
Android 7.0 锁屏解锁之向上滑动显示解锁界面分析 by jing.chen 锁屏的解锁操作是在锁屏界面向上滑动实现的,通过向上滑动调出解锁界面(如图案.PIN.密码解锁界面),在解锁界面输入 ...
- iphone手机锁屏密码怎么解锁?苹果手机如何解除屏幕锁
iphone手机锁屏密码怎么解锁?你输入正确的密码并解锁手机是正常的,但在这种情况下,如果你输入错误的密码超过10次,那么iPhone将被禁用,并弹出一个消息 "iPhone被禁用,连接到i ...
- 高仿IPhone滑动锁屏
下面的网址是我在eoe上面发表的帖子,源代码在里面,可以去下载. http://www.eoeandroid.com/thread-239655-1-1.html 最近公司在锁屏功能,类似小米的百变锁 ...
- android仿iphone页面,Android仿苹果关机界面实现代码
本文实例为大家分享了Android仿苹果关机界面的具体代码,供大家参考,具体内容如下 主class 用来控制viewdialog的显示 package com.android.server.polic ...
- iPhone忘记锁屏密码,多次输错被禁用?三种方法轻松解决!
为了保证手机的隐私安全,我们通常会将手机设置锁屏密码.最近有许多果粉反馈,自己长期不用的iPhone 忘记锁屏密码打不开了,多次输错密码还被禁用了,怎么办? 今天,小编就针对苹果手机忘记锁屏密码这个问 ...
- Android安全学习笔记1——锁屏密码方式
前言 在Android安全学习中,我接触到第一个例子是锁屏密码.我们日常使用手机的时候使用最多的锁屏密码是怎么构成的?下面分享一下我接触到的知识. 锁屏密码的思考 为了安全,Android设备在解锁屏 ...
- iphonex重量_精仿苹果iPhone X手机配置介绍
精仿苹果iPhone X手机配置介绍 [上市时间] 2017年10月最新版 [屏幕色彩] 1600万 [分 辨 率] 1920X1080 [屏幕尺寸] 5.8英寸IPS全视角电容式触摸屏 [处 理 器 ...
- android 监听屏幕是否锁屏
今天,简单讲讲如何监听手机屏幕是否锁屏. 实现方法:1)通过BroadcastReceiver接收广播Intent.ACTION_SCREEN_ON和Intent.ACTION_SCREEN_OFF可 ...
最新文章
- js两个小技巧【看到了就记录一下】
- Vue工程模板文件 webpack打包
- Eclipse上安装maven插件时出错
- 格雷码Gray Code详解
- Atitit 安全规范 指南 常见五种意外防止规范 attilax总结
- Python:第六次全国人口普查数据分析及可视化(pandas、matplotlib)
- mxm智能教育机器人无法智能对话_关于智能语音机器人使用中可能出现的问题
- EasyUI仓库管理系统
- 学习 Java全栈工程师6.0 初学者笔记1 2021-08-09
- 中英文电子书免费下载网站大全
- Python学习_案例for循环嵌套
- ES6 里的symbol
- Datawhale打卡活动 Kaggle Spaceship Titanic Day3
- ReentrantReadWriteLock、StampedLock读写锁
- CIA3 NOI接站(tarjan缩环+Floyd传递闭包+可相交最小路径覆盖)
- LINQ之Update
- 关于组织2021-2022全国青少年电子信息 智能创新大赛西北赛区(陕西)复赛的通知
- 需要计算机安装msxml,Win7安装Office2010提示让安装MSXML组件的五种解决方法
- 12槽10极分数槽集中绕组永磁电机结构讲解
- 录屏鼠标光标圆圈如何实现_自定义录制时鼠标形状
热门文章
- 全国高校人工智能与大数据创新联盟,首推云创高校大数据与人工智能实验室建设方案...
- 中国大学MOOC课程《Python语言程序设计》第五章 七段数码管绘制 引发的时间问题和海龟速度问题
- ROCKET PROPULSION ELEMENTS——CLASSIFICATION笔记
- C#:实现九九乘法表算法(附完整源码)
- java的gettime,java.util.Calendar.getTime()方法实例
- UGUI-- Scrollview 滚动视图
- 为什么程序员应该从现在就开始看书
- 用函数编写十进制转化为二进制
- 数字营销闭环解决方案
- Python爬虫深入 爬取当当网商品基本信息