首页 > 代码库 > android--仿苹果数字解锁功能
android--仿苹果数字解锁功能
跟着我一起按步骤来做,保证你一学就会。
步骤如下:
一、先自定义一个键盘布局文件:
在项目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--仿苹果数字解锁功能
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。