天天看點

BottomSheetDialog自定義鍵盤

在網上看了很多自定義鍵盤的,根據自己的需求加上了BottomSheetDialog來作為顯示容器,效果還是不錯的,直接上圖看效果

BottomSheetDialog自定義鍵盤

一、XML

1、數字鍵盤

<?xml version="1.0" encoding="utf-8"?>
<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="-5"  android:keyIcon="@android:drawable/ic_input_delete"  android:isRepeatable="true"/>
    </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="-11"  android:keyLabel="←" android:isRepeatable="true"/>

    </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="-12"  android:keyLabel="→" android:isRepeatable="true"/>

    </Row>
    <Row>
        <Key android:codes="-2"  android:keyLabel="abc"/>
        <Key android:codes="48"  android:keyLabel="0"/>
        <Key android:codes="-7"  android:keyLabel="符"/>
        <Key android:codes="-3"  android:keyLabel="完成"/>
    </Row>
</Keyboard>
           

2、英文鍵盤

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyHeight="50dp"
    android:keyWidth="9.45%p"
    android:horizontalGap="0.5%p"
    android:verticalGap="1dp">
    <Row>
        <Key android:codes="113"  android:keyEdgeFlags="left" android:keyLabel="q"/>
        <Key android:codes="119"  android:keyLabel="w"/>
        <Key android:codes="101"  android:keyLabel="e"/>
        <Key android:codes="114"  android:keyLabel="r"/>
        <Key android:codes="116"  android:keyLabel="t"/>
        <Key android:codes="121"  android:keyLabel="y"/>
        <Key android:codes="117"  android:keyLabel="u"/>
        <Key android:codes="105"  android:keyLabel="i"/>
        <Key android:codes="111"  android:keyLabel="o"/>
        <Key android:codes="112"  android:keyLabel="p"/>
    </Row>
    <Row>
        <Key android:codes="97" android:horizontalGap="5.225%p"
            android:keyEdgeFlags="left" android:keyLabel="a"/>
        <Key android:codes="115"  android:keyLabel="s"/>
        <Key android:codes="100"  android:keyLabel="d"/>
        <Key android:codes="102"  android:keyLabel="f"/>
        <Key android:codes="103"  android:keyLabel="g"/>
        <Key android:codes="104"  android:keyLabel="h"/>
        <Key android:codes="106"  android:keyLabel="j"/>
        <Key android:codes="107"  android:keyLabel="k"/>
        <Key android:codes="108"  android:keyLabel="l"/>
    </Row>
    <Row>
        <Key android:codes="-1" android:keyEdgeFlags="left"
            android:keyLabel="大小寫" android:keyWidth="15%p"/>
        <Key android:codes="122"  android:keyLabel="z" />
        <Key android:codes="120"  android:keyLabel="x"  />
        <Key android:codes="33"   android:keyLabel="c"  />
        <Key android:codes="118"  android:keyLabel="v" />
        <Key android:codes="98"   android:keyLabel="b" />
        <Key android:codes="110"  android:keyLabel="n"  />
        <Key android:codes="109"  android:keyLabel="m"  />
        <Key android:codes="-5"   android:keyEdgeFlags="right" android:horizontalGap="2dp"
            android:keyIcon="@android:drawable/ic_input_delete" android:keyWidth="15%p" android:isRepeatable="true"/>
    </Row>
    <Row android:rowEdgeFlags="bottom">
        <Key android:codes="-2"  android:keyLabel="123" android:keyEdgeFlags="left" android:keyWidth="15%p"/>
        <Key android:codes="44"  android:keyLabel=","/>
        <Key android:codes="-11" android:keyLabel="←"/>
        <Key android:codes="32"  android:isRepeatable="true" android:horizontalGap="2dp"
            android:keyLabel=" " android:keyWidth="29.35%p"/>
        <Key android:codes="-12" android:keyLabel="→" />
        <Key android:codes="46"  android:keyLabel="."/>
        <Key android:codes="-3"  android:keyEdgeFlags="right" android:keyLabel="完成" android:keyWidth="15%p"/>
    </Row>
</Keyboard>
           

3、符号鍵盤

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyHeight="50dp"
    android:keyWidth="8.33334444%p"
    android:verticalGap="6dp">
    <Row`>
        <Key android:codes="12304" android:horizontalGap="2dp" android:keyEdgeFlags="left"
            android:keyLabel="【"/>
        <Key android:codes="12305" android:horizontalGap="6dp" android:keyLabel="】"/>
        <Key android:codes="123" android:horizontalGap="6dp" android:keyLabel="{"/>
        <Key android:codes="125" android:horizontalGap="6dp" android:keyLabel="}"/>
        <Key android:codes="35" android:horizontalGap="6dp" android:keyLabel="#"/>
        <Key android:codes="37" android:horizontalGap="6dp" android:keyLabel="%"/>
        <Key android:codes="94" android:horizontalGap="6dp" android:keyLabel="^"/>
        <Key android:codes="42" android:horizontalGap="6dp" android:keyLabel="*"/>
        <Key android:codes="43" android:horizontalGap="6dp" android:keyLabel="+"/>
        <Key android:codes="61" android:horizontalGap="6dp" android:keyEdgeFlags="right"
            android:keyLabel="="/>
    </Row>
    <Row>
        <Key android:codes="95" android:keyEdgeFlags="left" android:horizontalGap="2dp"
            android:keyLabel="_"/>
        <Key android:codes="-9" android:horizontalGap="6dp" android:keyLabel="——"/>
        <Key android:codes="92" android:horizontalGap="6dp" android:keyLabel="\"/>
        <Key android:codes="124" android:horizontalGap="6dp" android:keyLabel="|"/>
        <Key android:codes="126" android:horizontalGap="6dp" android:keyLabel="~"/>
        <Key android:codes="12298" android:horizontalGap="6dp" android:keyLabel="《"/>
        <Key android:codes="12299" android:horizontalGap="6dp" android:keyLabel="》"/>
        <Key android:codes="36" android:horizontalGap="6dp" android:keyLabel="$"/>
        <Key android:codes="38" android:horizontalGap="6dp" android:keyLabel="&amp;"/>
        <Key android:codes="46" android:horizontalGap="6dp" android:keyEdgeFlags="right"
            android:keyLabel="·"/>
    </Row>
    <Row>
        <Key android:codes="-7" android:keyEdgeFlags="left" android:horizontalGap="2dp"
            android:keyLabel="123"
            android:keyWidth="11.000002%p"/>
        <Key android:codes="-8" android:horizontalGap="14dp" android:keyLabel="..."
            android:keyWidth="8.33334444%p"/>
        <Key android:codes="44" android:keyLabel="," android:horizontalGap="6dp"
            android:keyWidth="8.33334444%p"/>
        <Key android:codes="-10" android:keyLabel="^_^" android:horizontalGap="6dp"
            android:keyWidth="8.33334444%p"/>
        <Key android:codes="63" android:keyLabel="\?" android:horizontalGap="6dp"
            android:keyWidth="8.33334444%p"/>
        <Key android:codes="33" android:keyLabel="!" android:horizontalGap="6dp"
            android:keyWidth="8.33334444%p"/>
        <Key android:codes="-13" android:keyLabel="^o^" android:horizontalGap="6dp"
            android:keyWidth="8.33334444%p"/>
        <Key android:codes="-14" android:keyLabel="&gt;_&lt;" android:horizontalGap="6dp"
            android:keyWidth="8.33334444%p"/>
        <Key android:codes="-5" android:keyEdgeFlags="right" android:horizontalGap="14dp"
            android:keyIcon="@android:drawable/ic_input_delete"
            android:keyWidth="11.000002%p"/>
    </Row>
    <Row android:rowEdgeFlags="bottom">
        <Key android:codes="-2" android:keyLabel="abc" android:keyEdgeFlags="left"
            android:horizontalGap="2dp"
            android:keyWidth="11.000002%p"/>
        <Key android:codes="44" android:keyLabel="," android:horizontalGap="6dp"
            android:keyWidth="8.999998%p"/>
        <Key android:codes="-11" android:keyLabel="←" android:horizontalGap="6dp"
            android:keyWidth="8.999998%p"/>
        <Key android:codes="32" android:isRepeatable="true" android:horizontalGap="6dp"
            android:keyLabel=" "
            android:keyWidth="20.999996%p"/>
        <Key android:codes="-12" android:horizontalGap="6dp" android:keyLabel="→"
            android:keyWidth="8.999998%p"/>
        <Key android:codes="46" android:keyLabel="." android:horizontalGap="6dp"
            android:keyWidth="8.999998%p"/>
        <Key android:codes="-3" android:keyEdgeFlags="right" android:keyLabel="完成"
            android:horizontalGap="6dp"
            android:keyWidth="20.000002%p"/>
    </Row>
</Keyboard>
           

二、KeyboardUtil

import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.support.design.widget.BottomSheetDialog;
import android.text.Editable;
import android.widget.EditText;

public class KeyboardUtil {
    private KeyboardView keyboardView;
    private Keyboard k1;// 字母鍵盤
    private Keyboard k2;// 數字鍵盤
    private Keyboard k3;// 符号鍵盤
    private boolean isNum = false;// 是否資料鍵盤
    private boolean isUpper = false;// 是否大寫
    private boolean isSymbol = false;// 是否符号

    private static final int SYMBOL_CODE = -7;//符号鍵盤
    private static final int ELLIPSES_CODE = -8;//省略号
    private static final int CHINESE_HORIZONTAL_LINE_CODE = -9;//中文橫線
    private static final int SMILING_FACE_CODE = -10;//笑臉
    private static final int LEFT_CODE = -11;//中文橫線
    private static final int RIGHT_CODE = -12;//中文橫線
    private static final int HEE_CODE = -13;//哈哈
    private static final int AWKWARD_CODE = -14;//尴尬

    private BottomSheetDialog bottomSheet;
    private EditText ed;


    private KeyboardUtil(Activity activity, EditText edit) {
        this.ed = edit;
        k1 = new Keyboard(activity, R.xml.letter);
        k2 = new Keyboard(activity, R.xml.number);
        k3 = new Keyboard(activity, R.xml.symbol);

        keyboardView = new KeyboardView(activity, null);
        keyboardView.setKeyboard(k1);
        keyboardView.setEnabled(true);
        keyboardView.setPreviewEnabled(false);
        keyboardView.setOnKeyboardActionListener(onKeyboardActionListener);

        bottomSheet = new BottomSheetDialog(activity);//執行個體化BottomSheetDialog
        bottomSheet.setCancelable(true);//設定點選外部是否可以取消
        bottomSheet.setContentView(keyboardView);//設定對框框中的布局

    }

    OnKeyboardActionListener onKeyboardActionListener = 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) {
            Editable editable = ed.getText();
            int start = ed.getSelectionStart();
            if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
                bottomSheet.dismiss();
            } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
                if (editable != null && editable.length() > 0) {
                    if (start > 0) {
                        editable.delete(start - 1, start);
                    }
                }
            } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小寫切換
                isUpper = !isUpper;
                k1.setShifted(isUpper);
                keyboardView.invalidateAllKeys();
            } else if (primaryCode == SYMBOL_CODE) {// 符号鍵盤
                if (isSymbol) {
                    isSymbol = false;
                    keyboardView.setKeyboard(k2);
                } else {
                    isSymbol = true;
                    keyboardView.setKeyboard(k3);
                }
            } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 數字鍵盤切換
                if (isNum) {
                    isNum = false;
                    keyboardView.setKeyboard(k1);
                } else {
                    isNum = true;
                    keyboardView.setKeyboard(k2);
                }
            } else if (primaryCode == LEFT_CODE) { //向左
                if (start > 0) {
                    ed.setSelection(start - 1);
                }
            } else if (primaryCode == RIGHT_CODE) { // 向右
                if (start < ed.length()) {
                    ed.setSelection(start + 1);
                }
            } else if (primaryCode == ELLIPSES_CODE) { //省略号
                editable.insert(start, "...");
            } else if (primaryCode == CHINESE_HORIZONTAL_LINE_CODE) {
                editable.insert(start, "——");
            } else if (primaryCode == SMILING_FACE_CODE) {
                editable.insert(start, "^_^");
            } else if (primaryCode == HEE_CODE) {
                editable.insert(start, "^o^");
            } else if (primaryCode == AWKWARD_CODE) {
                editable.insert(start, ">_<");
            } else {
                String str = Character.toString((char) primaryCode);
                if (isWord(str)) {
                    if (isUpper) {
                        str = str.toUpperCase();
                    } else {
                        str = str.toLowerCase();
                    }
                }
                editable.insert(start, str);

            }
        }
    };

    private boolean isShow = false;

    public void showKeyboard(Context context) {
            bottomSheet.show();//顯示彈窗
            isShow = true;
    }

    private boolean isWord(String str) {
        return str.matches("[a-zA-Z]");
    }

    private static KeyboardUtil mInstance;

    public static KeyboardUtil shared(Activity activity, EditText edit) {
        if (mInstance == null) {
            mInstance = new KeyboardUtil(activity, edit);
        }
        mInstance.ed = edit;
        return mInstance;
    }

}

           

三、MainActivity中的使用

TextInputEditText textInputEditText=findViewById(R.id.textInputEditText);
Util.disableShowSoftInput(textInputEditText);//禁止Edittext彈出軟體盤,光标依然正常顯示

textInputEditText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        KeyboardUtil.shared(TestActivity.this,textInputEditText).showKeyboard(TestActivity.this);
    }
});
           

四、禁止彈出軟體盤

/**
 * 禁止Edittext彈出軟體盤,光标依然正常顯示。
 */
public static void disableShowSoftInput(EditText editTest) {
    if (android.os.Build.VERSION.SDK_INT <= 10) {
        editTest.setInputType(InputType.TYPE_NULL);
    } else {
        Class<EditText> cls = EditText.class;
        Method method;
        try {
            method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
            method.setAccessible(true);
            method.invoke(editTest, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            method = cls.getMethod("setSoftInputShownOnFocus", boolean.class);
            method.setAccessible(true);
            method.invoke(editTest, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}