Android自定義鍵盤

這是之前公司項目,需要用身份證來查詢,絕大部分人的身份證是純數字的,這個可以直接在EditText設置inputType,輸入法彈出時會自動切換到數字鍵盤,但是,碰到身份證含有X的,這就麻煩了,得手動切換到英文輸入界面,然後還得再切換回來,甚至還牽扯到大小寫處理,雖然不算是個問題,但是互聯網思維嘛,人性化!所以,本着不怕麻煩的精神,還是學習了自定義個鍵盤來提升用戶體驗

需要說的東西感覺不多

老規矩,先看效果:
這裏寫圖片描述

首先,先新建個xml(應該一看就明白,沒什麼可說的),來定義我們鍵盤上要擺放的東西(codes爲ASCII碼)

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyHeight="50dp"
    android:keyWidth="25%p">
    <Row>
        <Key
            android:codes="49"
            android:keyLabel="1" />
        <Key
            android:codes="50"
            android:keyLabel="2" />
        <Key
            android:codes="51"
            android:keyLabel="3" />
        <Key
            android:codes="9995"
            android:keyEdgeFlags="right"
            android:keyLabel="重輸" />
    </Row>
    <Row>
        <Key
            android:codes="52"
            android:keyLabel="4" />
        <Key
            android:codes="53"
            android:keyLabel="5" />
        <Key
            android:codes="54"
            android:keyLabel="6" />
        <Key
            android:codes="-5"
            android:keyLabel="刪除"
            android:isRepeatable="true"
            android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key
            android:codes="55"
            android:keyLabel="7" />
        <Key
            android:codes="56"
            android:keyLabel="8" />
        <Key
            android:codes="57"
            android:keyLabel="9" />
        <Key
            android:codes="88"
            android:keyLabel="X"
            android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key
            android:codes="9994"
            android:keyLabel="←" />
        <Key
            android:codes="48"
            android:keyLabel="0" />
        <Key
            android:codes="9996"
            android:keyLabel="→" />
        <Key
            android:codes="-4"
            android:keyLabel="√"
            android:keyEdgeFlags="right"/>
    </Row>
</Keyboard>

然後新建個類,CustomKeyboard,來控制我們按鍵的操作

public class CustomKeyboard {
    private EditText mEdittext;
    private KeyboardView mKeyboardView;
    private Keyboard mKeyboard;
    private IKeyboardFinish mIKeyboardFinish;

    public CustomKeyboard(Context context, KeyboardView keyboardView, EditText editText, IKeyboardFinish iKeyboardFinish){
        this.mEdittext = editText;
        this.mIKeyboardFinish = iKeyboardFinish;
        mKeyboard = new Keyboard(context, R.xml.keyboard);//從xml中加載自定義的鍵盤
        mKeyboardView = keyboardView;
        mKeyboardView.setKeyboard(mKeyboard);
        mKeyboardView.setPreviewEnabled(false);
        mKeyboardView.setOnKeyboardActionListener(actionListener);
    }
    private KeyboardView.OnKeyboardActionListener actionListener = new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void onPress(int primaryCode) {
        }

        @Override
        public void onRelease(int primaryCode) {

        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            Editable editable = mEdittext.getText();
            int index = mEdittext.getSelectionStart();//光標位置
            switch (primaryCode){

                case Keyboard.KEYCODE_DELETE://回退
                    if (editable != null && editable.length() > 0){
                        if (index > 0){
                            editable.delete(index - 1,index);
                        }
                    }
                    break;
                case Keyboard.KEYCODE_DONE://完成
                    //hideKeyboard();
                    mIKeyboardFinish.inputFinish();//回調
                    break;
                case 9995://重輸
                    mEdittext.setText("");
                    break;
                case 9994://左移
                    if (index > 0){
                        mEdittext.setSelection(index - 1);
                    }
                    break;
                case 9996://右移
                    if (index < mEdittext.length()){
                        mEdittext.setSelection(index + 1);
                    }
                    break;
                default:
                    editable.insert(index,Character.toString((char)primaryCode));
                    break;
            }
        }

        @Override
        public void onText(CharSequence text) {

        }

        @Override
        public void swipeLeft() {

        }

        @Override
        public void swipeRight() {

        }

        @Override
        public void swipeDown() {

        }

        @Override
        public void swipeUp() {

        }
    };

    public void showKeyboard(){
        if(mKeyboardView.getVisibility() != View.VISIBLE){
            mKeyboardView.setVisibility(View.VISIBLE);
        }
    }

    public void hideKeyboard(){
        if (mKeyboardView.getVisibility() == View.VISIBLE){
            mKeyboardView.setVisibility(View.GONE);
        }
    }
}

定義個回調接口:

public interface IKeyboardFinish {
    void inputFinish();
}

然後在Activity的佈局文件中定義

<android.inputmethodservice.KeyboardView
            android:id="@+id/customKeyboard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:shadowRadius="1.0" />

後臺關鍵代碼:

private MaterialEditText edtNum;//大名鼎鼎的MaterialEditText,github上一搜便知
private Context mContext = this;
private CustomKeyboard mCustomKeyboard;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edtNum = (MaterialEditText) findViewById(R.id.edtNum);
    //屏蔽掉系統默認輸入法
    if (Build.VERSION.SDK_INT <= 10) {
        edtNum.setInputType(InputType.TYPE_NULL);
    } else {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        try {
            Class<EditText> cls = EditText.class;
            Method setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
            setShowSoftInputOnFocus.setAccessible(true);
            setShowSoftInputOnFocus.invoke(edtNum, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //初始化鍵盤,傳入回調接口,以便在此處進行完成後的操作。默認就顯示鍵盤
    KeyboardView keyboardView = (KeyboardView) findViewById(R.id.customKeyboard);
    mCustomKeyboard = new CustomKeyboard(mContext, keyboardView, edtNum, iKeyboardFinish);
    mCustomKeyboard.showKeyboard();
    edtNum.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mCustomKeyboard.showKeyboard();
            return false;
        }
    });
}

/**
 * 輸入完成
 */
private IKeyboardFinish iKeyboardFinish = new IKeyboardFinish() {
    @Override
    public void inputFinish() {
        String idcard = edtNum.getText().toString();
        Toast.makeText(mContext,idcard,Toast.LENGTH_SHORT).show();

    }
};

PS:鍵盤是android默認的樣式,沒有研究怎麼改樣式,知道的大xiong弟麻煩留言說說唄

代碼:https://github.com/DonnyHe/IDcardKeyboard

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章