標籤: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() {@Overridepublic void swipeUp() {}@Overridepublic void swipeRight() {}@Overridepublic void swipeLeft() {}@Overridepublic void swipeDown() {}@Overridepublic void onText(CharSequence text) {}@Overridepublic void onRelease(int primaryCode) {}@Overridepublic void onPress(int primaryCode) {}@Overridepublic 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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {}@Overridepublic void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3) {}@Overridepublic void afterTextChanged(Editable arg0) {if (etPwdFour.getText() != null&& etPwdFour.getText().toString().length() >= 1) {new Thread(new Runnable() {@Overridepublic 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() {@Overridepublic 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--仿蘋果數字解鎖功能