Android 自定義軟鍵盤

一、 Android 自定義軟鍵盤開發(fā)流程
  1. 建立軟鍵盤樣式
    ? ? ? ?即在項目res文件夾中創(chuàng)建所需要的各種軟鍵盤的樣式(比如說:字母、數(shù)字、符號 等)。
  2. 創(chuàng)建layout布局文件(非必須)
    ? ? ? ?在布局文件中給軟鍵盤創(chuàng)建container,以便顯示軟鍵盤
  3. 自定義KeyboardView
    ? ? ? ?自定義一個KeyboardView 并繼承自KeyboardView,在自定義的KeyboardView中繪制特殊按鍵,包括按鍵的點擊背景,圖片,文字 等。
  4. 自定義一個普通java類,一般取名為 **Keyboard.java
    ? ? ? ?把軟鍵盤加載到container中,即在布局文件里預(yù)留的存放軟鍵盤 的container。
    ? ? ? ?在類的內(nèi)部實現(xiàn)軟鍵盤的輸入控制,鍵盤轉(zhuǎn)換控制,軟鍵盤的顯示與隱藏控制 等。
    ? ? ? ?在需要用到軟鍵盤的Activity中實例化該Keyboard 類,并傳入必要的數(shù)據(jù)和信息。
二、建立軟鍵盤樣式

? ? ? ?建立軟鍵盤樣式可以直接通過xml進行排版,在res/xml中創(chuàng)建一個根節(jié)點為Keyboard的xml就可以開始排版了
這個xml中屬性如下:

Keyboard.Key 屬性 介紹
android:codes 此鍵輸出的unicode值或逗號分隔值。
android:horizontalGap 鍵之間的默認水平間隙。
android:iconPreview 彈出預(yù)覽中顯示的圖標。
android:isModifier 這是否是修改鍵,如Alt或Shift。
android:isRepeatable 是否長按此鍵會使其重復(fù)。
android:isSticky 這是否是切換鍵。
android:keyEdgeFlags 關(guān)鍵邊緣標志。
android:keyHeight 鍵的默認高度,以像素為單位或顯示寬度的百分比。
android:keyIcon 要在鍵上顯示的圖標而不是標簽。
android:keyLabel 要在鍵上顯示的標簽。
android:keyOutputText 按下此鍵時要輸出的字符串。
android:keyWidth 鍵的默認寬度,以像素為單位或顯示寬度的百分比。
android:popupCharacters 要在彈出鍵盤中顯示的字符。
android:popupKeyboard 任何彈出鍵盤的XML鍵盤布局。

實例代碼:
數(shù)字鍵盤

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="2.5%p"
    android:keyHeight="6%p"
    android:keyWidth="30%p"
    android:verticalGap="10px">
    <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="ABC" />
        <Key android:codes="48" android:keyLabel="0" />
        <Key android:codes="-35" android:isRepeatable="true" />
    </Row>
</Keyboard>

英文鍵盤:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="1%p"
    android:keyHeight="6%p"
    android:keyWidth="10%p"
    android:verticalGap="10px">
    <Row>
        <Key android:codes="113" android:keyEdgeFlags="left" android:keyLabel="q" android:keyWidth="8.9%p" />
        <Key android:codes="119" android:keyLabel="w" android:keyWidth="8.9%p" />
        <Key android:codes="101" android:keyLabel="e" android:keyWidth="8.9%p" />
        <Key android:codes="114" android:keyLabel="r" android:keyWidth="8.9%p" />
        <Key android:codes="116" android:keyLabel="t" android:keyWidth="8.9%p" />
        <Key android:codes="121" android:keyLabel="y" android:keyWidth="8.9%p" />
        <Key android:codes="117" android:keyLabel="u" android:keyWidth="8.9%p" />
        <Key android:codes="105" android:keyLabel="i" android:keyWidth="8.9%p" />
        <Key android:codes="111" android:keyLabel="o" android:keyWidth="8.9%p" />
        <Key android:codes="112" android:keyEdgeFlags="right" android:keyLabel="p" android:keyWidth="8.9%p" />
    </Row>
    <Row>
        <Key android:codes="97" android:horizontalGap="5.5%p" android:keyEdgeFlags="left" android:keyLabel="a" android:keyWidth="9%p" />
        <Key android:codes="115" android:keyLabel="s" android:keyWidth="9%p" />
        <Key android:codes="100" android:keyLabel="d" android:keyWidth="9%p" />
        <Key android:codes="102" android:keyLabel="f" android:keyWidth="9%p" />
        <Key android:codes="103" android:keyLabel="g" android:keyWidth="9%p" />
        <Key android:codes="104" android:keyLabel="h" android:keyWidth="9%p" />
        <Key android:codes="106" android:keyLabel="j" android:keyWidth="9%p" />
        <Key android:codes="107" android:keyLabel="k" android:keyWidth="9%p" />
        <Key android:codes="108" android:keyEdgeFlags="right" android:keyLabel="l" android:keyWidth="9%p" />
    </Row>
    <Row>
        <Key android:codes="-1" android:isModifier="true" android:isSticky="true" android:keyEdgeFlags="left" android:keyWidth="13%p" />
        <Key android:codes="122" android:horizontalGap="1.5%p" android:keyLabel="z" android:keyWidth="9%p" />
        <Key android:codes="120" android:keyLabel="x" android:keyWidth="9%p" />
        <Key android:codes="99" android:keyLabel="c" android:keyWidth="9%p" />
        <Key android:codes="118" android:keyLabel="v" android:keyWidth="9%p" />
        <Key android:codes="98" android:keyLabel="b" android:keyWidth="9%p" />
        <Key android:codes="110" android:keyLabel="n" android:keyWidth="9%p" />
        <Key android:codes="109" android:keyLabel="m" android:keyWidth="9%p" />
        <Key android:codes="-5" android:horizontalGap="1.5%p" android:isRepeatable="true" android:keyWidth="13%p" />
    </Row>
    <Row android:rowEdgeFlags="bottom">
        <Key android:codes="-2" android:keyLabel="123" android:keyWidth="19%p" />
        <Key android:codes="32" android:isRepeatable="false" android:keyLabel="space" android:keyWidth="58%p" />
        <Key android:codes="100860" android:keyEdgeFlags="right" android:keyLabel="#+=" android:keyWidth="19%p" />
    </Row>
</Keyboard>
三、自定義KeyboardView

? ? ? ?如果你對KeyboardView的按鍵有特定的一些樣式的話,那你就要自定義KeyboardView了。
? ? ? ?自定義KeyboardView最重要的就是重寫onDraw方法,同時注意一定要寫繪制背景再繪制文本。List<Keyboard.Key> keys = getKeyboard().getKeys();這個是獲取所有按鍵信息Keyboard.Key,其中Key.codes是獲取xml中設(shè)定的code,可以根據(jù)這個判定需要繪制的樣式不同。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.support.annotation.DrawableRes;
import android.util.AttributeSet;

import java.lang.reflect.Field;
import java.util.List;

public class CustomKeyboardView extends KeyboardView {

    private Context context;
    private Paint paint;
    private Rect bounds;

    public CustomKeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        bounds = new Rect();
        this.context = context;
    }

    public CustomKeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        bounds = new Rect();
        this.context = context;
    }

    /**
     * 重寫這個方法是為了可以繪制一些特殊按鍵
     * @param canvas
     */
    @Override
    public void onDraw(Canvas canvas) {
        List<Keyboard.Key> keys = getKeyboard().getKeys();
        for (Keyboard.Key key : keys) {
            if(key.codes[0] != -5 && key.codes[0] != -4){
                //繪制普通信息文本的背景
                drawBackground(R.drawable.keyboard_bg,canvas,key);
                //繪制普通信息的文本信息
                drawText(canvas,key);
            }else{
                //繪制特殊按鍵
                drawSpecialKey(canvas,key);
            }
        }
    }

    private void drawSpecialKey(Canvas canvas, Keyboard.Key key) {
        if(key.codes[0] == -5){
            drawBackground(R.drawable.keyboard_bg,canvas,key);
            //繪制設(shè)置了icon的按鈕
            key.icon.setBounds(key.x + (key.width - key.icon.getIntrinsicWidth()) / 2,
                    key.y + (key.height - key.icon.getIntrinsicHeight()) / 2,
                    key.x + (key.width - key.icon.getIntrinsicWidth()) / 2 + key.icon.getIntrinsicWidth(),
                    key.y + (key.height - key.icon.getIntrinsicHeight()) / 2 + key.icon.getIntrinsicHeight());
            key.icon.draw(canvas);
        }else if(key.codes[0] == -4){
            drawBackground(R.drawable.white,canvas,key);
        }
    }

    private void drawBackground(@DrawableRes int drawableId, Canvas canvas, Keyboard.Key key) {
        Drawable drawable = context.getResources().getDrawable(drawableId);
        int[] state = key.getCurrentDrawableState();
        if (key.codes[0] != 0) {
            drawable.setState(state);
        }
        drawable.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
        drawable.draw(canvas);
    }

    //繪制文本
    private void drawText(Canvas canvas, Keyboard.Key key) {
        if(key.label != null){
            String label = key.label.toString();
            Field field;
            int keyTextSize;
            try {
                //獲取KeyboardView設(shè)置的默認文本字體大小
                field = KeyboardView.class.getDeclaredField("mLabelTextSize");
                field.setAccessible(true);
                keyTextSize = (int) field.get(this);
                paint.setTextSize(keyTextSize);
                paint.setTypeface(Typeface.DEFAULT);
                paint.getTextBounds(label,0,label.length(),bounds);
                canvas.drawText(label,key.x + (key.width / 2), (key.y + key.height / 2) + bounds.height() / 2, paint);
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}
四、自定義一個KeyBoard的幫助類

這個類里面主要的作用是使用我們自定義的KeyboardView代替系統(tǒng)自帶的KeyboardView進行展示,這里的話主要是需要在EditText獲取焦點時候講彈出軟鍵盤設(shè)定成自定義的。

import android.annotation.SuppressLint;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Build;
import android.text.Editable;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class KeyboardUtil {

    private Context mContext;
    private KeyboardView mKeyboardView;
    private EditText mEditText;


    @SuppressLint("ClickableViewAccessibility")
    public KeyboardUtil(Context context, KeyboardView keyboardView, EditText editText) {
        this.mContext = context;
        this.mKeyboardView = keyboardView;
        this.mEditText = editText;
        initKeyboard();
        mEditText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    hideSystemKeyboard((EditText) v);
                    mKeyboardView.setVisibility(View.VISIBLE);
                }
                return false;
            }
        });
        mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (v instanceof EditText) {
                    if (!hasFocus) {
                        mKeyboardView.setVisibility(View.GONE);
                    } else {
                        hideSystemKeyboard((EditText) v);
                        mKeyboardView.setVisibility(View.VISIBLE);
                    }
                }

            }
        });
    }

    private void hideSystemKeyboard(EditText v) {
        this.mEditText = v;
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(imm == null){
            return;
        }
        boolean isOpen = imm.isActive();
        if (isOpen) {
            imm.hideSoftInputFromWindow(v.getWindowToken(),0);
        }
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            v.setShowSoftInputOnFocus(false);
        }else{
            v.setInputType(0);
        }
    }

    private void initKeyboard() {
        Keyboard keyboard = new Keyboard(mContext,R.xml.number_keyboard);
        mKeyboardView.setKeyboard(keyboard);
        mKeyboardView.setEnabled(true);
        mKeyboardView.setOnKeyboardActionListener(listener);
    }

    private KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void onPress(int primaryCode) {
            if(primaryCode == -4 || primaryCode == -5){
                mKeyboardView.setPreviewEnabled(false);
            }else{
                mKeyboardView.setPreviewEnabled(true);
            }
        }

        @Override
        public void onRelease(int primaryCode) {

        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            Editable editable = mEditText.getText();
            int start = mEditText.getSelectionStart();
            int end = mEditText.getSelectionEnd();
            if(primaryCode == Keyboard.KEYCODE_DONE){
                mKeyboardView.setVisibility(View.GONE);
            }else if(primaryCode == Keyboard.KEYCODE_DELETE){
                if(editable != null && editable.length() > 0){
                    if(start == end){
                        editable.delete(start -1, start);
                    }else{
                        editable.delete(start,end);
                    }
                }
            }else{
                editable.replace(start,end,Character.toString((char) primaryCode));
            }
        }

        @Override
        public void onText(CharSequence text) {

        }

        @Override
        public void swipeLeft() {

        }

        @Override
        public void swipeRight() {

        }

        @Override
        public void swipeDown() {

        }

        @Override
        public void swipeUp() {

        }
    };
}

以上就是自定義軟件盤所有相關(guān)的操作,這個是比較簡單的,如果要負責的話,就需要多研究下,如果有啥沒寫好的,見諒一下,第一次寫。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 先上圖: 剛開始寫自定義軟鍵盤是走了點彎路,用控件去實現(xiàn),好多小細節(jié)處理起來真的太抓狂了… 后來發(fā)現(xiàn) 神控件 Ke...
    silence_jjj閱讀 4,784評論 0 0
  • 前言 ★本文簡述: 簡單通過KeyBoardView實現(xiàn)自定義鍵盤功能。 真的只是給和我一樣的渣渣簡單介紹,所以了...
    路人葵閱讀 29,106評論 11 20
  • 簡介 今天在掘金上看了一篇文章,實現(xiàn)自定義軟鍵盤,發(fā)現(xiàn)其實實現(xiàn)方式比較簡單,不需要改動系統(tǒng)api,只是單純的加載自...
    fushuang閱讀 4,651評論 1 14
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,284評論 4 61
  • 最近項目中在做一個股票交易需求升級, 產(chǎn)品對于輸入方式有一些特殊的要求, 具體就是對于輸入鍵盤加了諸多限制. 這就...
    kangqiao182閱讀 15,372評論 1 23

友情鏈接更多精彩內(nèi)容