android自定義密碼鍵盤
阿新 • • 發佈:2019-01-25
先看介面佈局檔案
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <EditText
- android:id="@+id/edit"
- android:layout_width
- android:layout_height="wrap_content"/>
- <EditText
- android:id="@+id/edit1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:password="true"/>
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height
- <android.inputmethodservice.KeyboardView
- android:id="@+id/keyboard_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:focusable="true"
- android:focusableInTouchMode
- android:background="@color/lightblack"
- android:keyBackground="@drawable/btn_keyboard_key"
- android:keyTextColor="@color/white"
- android:visibility="gone"/>
- </RelativeLayout>
- </LinearLayout>
通過佈局檔案可以看出介面上有兩個輸入框,其中一個是密碼輸入框,介面上還有一個隱藏的鍵盤控制元件。
在res下新建xml資料夾,在xml資料夾中新建qwerty.xml和symbols.xml檔案. qwerty.xml 是字母鍵盤佈局,symbols.xml 是數字鍵盤佈局,內如如下
qwerty.xml內容
- <?xmlversion="1.0"encoding="UTF-8"?>
- <Keyboardandroid:keyWidth="10.000002%p"android:keyHeight="@dimen/key_height"
- android:horizontalGap="0.0px"android:verticalGap="0.0px"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <Row>
- <Keyandroid:codes="113"android:keyEdgeFlags="left"
- android:keyLabel="q"/>
- <Keyandroid:codes="119"android:keyLabel="w"/>
- <Keyandroid:codes="101"android:keyLabel="e"/>
- <Keyandroid:codes="114"android:keyLabel="r"/>
- <Keyandroid:codes="116"android:keyLabel="t"/>
- <Keyandroid:codes="121"android:keyLabel="y"/>
- <Keyandroid:codes="117"android:keyLabel="u"/>
- <Keyandroid:codes="105"android:keyLabel="i"/>
- <Keyandroid:codes="111"android:keyLabel="o"/>
- <Keyandroid:codes="112"android:keyEdgeFlags="right"
- android:keyLabel="p"/>
- </Row>
- <Row>
- <Keyandroid:horizontalGap="4.999995%p"android:codes="97"
- android:keyEdgeFlags="left"android:keyLabel="a"/>
- <Keyandroid:codes="115"android:keyLabel="s"/>
- <Keyandroid:codes="100"android:keyLabel="d"/>
- <Keyandroid:codes="102"android:keyLabel="f"/>
- <Keyandroid:codes="103"android:keyLabel="g"/>
- <Keyandroid:codes="104"android:keyLabel="h"/>
- <Keyandroid:codes="106"android:keyLabel="j"/>
- <Keyandroid:codes="107"android:keyLabel="k"/>
- <Keyandroid:codes="108"android:keyEdgeFlags="right"
- android:keyLabel="l"/>
- </Row>
- <Row>
- <Keyandroid:keyWidth="14.999998%p"android:codes="-1"
- android:keyEdgeFlags="left"android:isModifier="true"
- android:isSticky="true"android:keyIcon="@drawable/sym_keyboard_shift"/>
- <Keyandroid:codes="122"android:keyLabel="z"/>
- <Keyandroid:codes="120"android:keyLabel="x"/>
- <Keyandroid:codes="99"android:keyLabel="c"/>
- <Keyandroid:codes="118"android:keyLabel="v"/>
- <Keyandroid:codes="98"android:keyLabel="b"/>
- <Keyandroid:codes="110"android:keyLabel="n"/>
- <Keyandroid:codes="109"android:keyLabel="m"/>
- <Keyandroid:keyWidth="14.999998%p"android:codes="-5"
- android:keyEdgeFlags="right"android:isRepeatable="true"
- android:keyIcon="@drawable/sym_keyboard_delete"/>
- </Row>
- <Rowandroid:rowEdgeFlags="bottom">
- <Keyandroid:keyWidth="20.000004%p"android:codes="-2"
- android:keyLabel="12#"/>
- <Keyandroid:keyWidth="14.999998%p"android:codes="44"
- android:keyLabel=","/>
- <Keyandroid:keyWidth="29.999996%p"android:codes="32"
- android:isRepeatable="true"android:keyIcon="@drawable/sym_keyboard_space"/>
- <Keyandroid:keyWidth="14.999998%p"android:codes="46"
- android:keyLabel="."/>
- <Keyandroid:keyWidth="20.000004%p"android:codes="-3"
- android:keyEdgeFlags="right"android:keyLabel="完成"/>
- </Row>
- </Keyboard>
symbols.xml 內容
- <?xmlversion="1.0"encoding="utf-8"?>
- <Keyboardxmlns:android="http://schemas.android.com/apk/res/android"
- android:keyWidth="25%p"android:horizontalGap="0px"
- android:verticalGap="0px"android:keyHeight="@dimen/key_height">
- <Row>
- <Keyandroid:codes="49"android:keyLabel="1"/>
- <Keyandroid:codes="50"android:keyLabel="2"/>
- <Keyandroid:codes="51"android:keyLabel="3"/>
- <Keyandroid:codes="57419"android:keyEdgeFlags="right"
- android:keyIcon="@drawable/sym_keyboard_left"/>
- </Row>
- <Row>
- <Keyandroid:codes="52"android:keyLabel="4"/>
- <Keyandroid:codes="53"android:keyLabel="5"/>
- <Keyandroid:codes="54"android:keyLabel="6"/>
- <Keyandroid:codes="57421"android:keyEdgeFlags="right"
- android:keyIcon="@drawable/sym_keyboard_right"/>
- </Row>
- <Row>
- <Keyandroid:codes="55"android:keyLabel="7"/>
- <Keyandroid:codes="56"android:keyLabel="8"/>
- <Keyandroid:codes="57"android:keyLabel="9"/>
- <Keyandroid:codes="-3"android:keyHeight="100dip"
- android:keyEdgeFlags="right"android:isRepeatable="true"
- android:keyLabel="完成"/>
- </Row>
- <Row>
- <Keyandroid:codes="-2"android:keyLabel="ABC"/>
- <Keyandroid:codes="48"android:keyLabel="0"/>
- <Keyandroid:codes="-5"android:keyIcon="@drawable/sym_keyboard_delete"/>
- </Row>
- </Keyboard>
KeydemoActivity.java
- package cn.key;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.text.InputType;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.widget.EditText;
- publicclass KeydemoActivity extends Activity {
- private Context ctx;
- private Activity act;
- private EditText edit;
- private EditText edit1;
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ctx = this;
- act = this;
- edit = (EditText) this.findViewById(R.id.edit);
- edit.setInputType(InputType.TYPE_NULL);
- edit1 = (EditText) this.findViewById(R.id.edit1);
- edit.setOnTouchListener(new OnTouchListener() {
- @Override
- publicboolean onTouch(View v, MotionEvent event) {
- new KeyboardUtil(act, ctx, edit).showKeyboard();
- returnfalse;
- }
- });
- edit1.setOnTouchListener(new OnTouchListener() {
- @Override
- publicboolean onTouch(View v, MotionEvent event) {
- int inputback = edit1.getInputType();
- edit1.setInputType(InputType.TYPE_NULL);
- new KeyboardUtil(act, ctx, edit1).showKeyboard();
- edit1.setInputType(inputback);
- returnfalse;
- }
- });
- }
- }
KeyboardUtil.java
- package cn.key;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.inputmethodservice.Keyboard;
- import android.inputmethodservice.KeyboardView;
- import android.inputmethodservice.Keyboard.Key;
- import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
- import android.text.Editable;
- import android.view.View;
- import android.widget.EditText;
- publicclass KeyboardUtil {
- private Context ctx;
- private Activity act;
- private KeyboardView keyboardView;
- private Keyboard k1;// 字母鍵盤
- private Keyboard k2;// 數字鍵盤
- publicboolean isnun = false;// 是否資料鍵盤
- publicboolean isupper = false;// 是否大寫
- private EditText ed;
- public KeyboardUtil(Activity act, Context ctx, EditText edit) {
- this.act = act;
- this.ctx = ctx;
- this.ed = edit;
- k1 = new Keyboard(ctx, R.xml.qwerty);
- k2 = new Keyboard(ctx, R.xml.symbols);
- keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
- keyboardView.setKeyboard(k1);
- keyboardView.setEnabled(true);
- keyboardView.setPreviewEnabled(true);
- keyboardView.setOnKeyboardActionListener(listener);
- }
- private OnKeyboardActionListener listener = new OnKeyboardActionListener() {
- @Override
- publicvoid swipeUp() {
- }
- @Override
- publicvoid swipeRight() {
- }
- @Override
- publicvoid swipeLeft() {
- }
- @Override
- publicvoid swipeDown() {
- }
- @Override
- publicvoid onText(CharSequence text) {
- }
- @Override
- publicvoid onRelease(int primaryCode) {
- }
- @Override
- publicvoid onPress(int primaryCode) {
- }
- @Override
- publicvoid onKey(int primaryCode, int[] keyCodes) {
- Editable editable = ed.getText();
- int start = ed.getSelectionStart();
- if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
- hideKeyboard();
- } elseif (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
- if (editable != null && editable.length() > 0) {
- if (start > 0) {
- editable.delete(start - 1, start);
- }
- }
- } elseif (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小寫切換
- changeKey();
- keyboardView.setKeyboard(k1);
- } elseif (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 數字鍵盤切換
- if (isnun) {
- isnun = false;
- keyboardView.setKeyboard(k1);
- } else {
- isnun = true;
- keyboardView.setKeyboard(k2);
- }
- } elseif (primaryCode == 57419) { // go left
- if (start > 0) {
- ed.setSelection(start - 1);
- }
- } elseif (primaryCode == 57421) { // go right
- if (start < ed.length()) {
- ed.setSelection(start + 1);
- }
- } else {
- editable.insert(start, Character.toString((char) primaryCode));
- }
- }
- };
- /**
- * 鍵盤大小寫切換
- */
- privatevoid changeKey() {
- List<Key> keylist = k1.getKeys();
- if (isupper) {//大寫切換小寫
- isupper = false;
- for(Key key:keylist){
- if (key.label!=null && isword(key.label.toString())) {
- key.label = key.label.toString().toLowerCase();
- key.codes[0] = key.codes[0]+32;
- }
- }
- } else {//小寫切換大寫
- isupper = true;
- for(Key key:keylist){
- if (key.label!=null && isword(key.label.toString())) {
- key.label = key.label.toString().toUpperCase();
- key.codes[0] = key.codes[0]-32;
- }
- }
- }
- }
- publicvoid showKeyboard() {
- int visibility = keyboardView.getVisibility();
- if (visibility == View.GONE || visibility == View.INVISIBLE) {
- keyboardView.setVisibility(View.VISIBLE);
- }
- }
- publicvoid hideKeyboard() {
- int visibility = keyboardView.getVisibility();
- if (visibility == View.VISIBLE) {
- keyboardView.setVisibility(View.INVISIBLE);
- }
- }
- privateboolean isword(String str){
- String wordstr = "abcdefghijklmnopqrstuvwxyz";
- if (wordstr.indexOf(str.toLowerCase())>-1) {
- returntrue;
- }
- returnfalse;
- }