Android自定义键盘(KeyboardView)
生活随笔
收集整理的這篇文章主要介紹了
Android自定义键盘(KeyboardView)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 1.場景:
- 2.想法:
- 3.開始實現:
- 《一》 在res包下創建xml目錄,Keyboard標簽來定義鍵盤布局:
- 《二》創建IKeyboardView類并繼承KeyboardView類,設置鍵盤布局(數字和字母)
- 《三》 處理自定義鍵盤按鍵的點擊事件以及預覽,并實現數字和字母鍵盤(包括大小寫)之間的切換
- 《四》 綁定EditText,并且屏蔽系統鍵盤(實現點擊切換綁定多個EditText)
- 《五》. 使用
- 4.KeyboardView,keyboard,Key,Row屬性解釋:
- 5.完整代碼
- 6.注意事項
- 6.結尾
1.場景:
項目中有定制的設備需要放在室外,用戶使用時使用系統自帶的鍵盤肯能沒有那么方便,所以就需要使用到了自定義鍵盤
(結尾附上完整代碼可以直接使用,不想看過程的可以直接跳過)
2.想法:
封裝成一個比較通用的,當成一個View一樣來使用
**實現自定義鍵盤思路:**1. 在res包下創建xml目錄,Keyboard標簽來定義鍵盤布局2. 創建IKeyboardView類并繼承KeyboardView類,設置鍵盤布局(數字和字母)3. 處理自定義鍵盤按鍵的點擊事件以及預覽,并實現數字和字母鍵盤(包括大小寫)之間的切換4. 綁定EditText,并且屏蔽系統鍵盤(實現點擊切換綁定多個EditText)5. 使用3.開始實現:
《一》 在res包下創建xml目錄,Keyboard標簽來定義鍵盤布局:
創建keyboard_num.xml文件(數字鍵盤布局)
<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:keyWidth="10%p"android:keyHeight="7%p"android:horizontalGap="0.0px"android:verticalGap="0.0px"><Row android:verticalGap="1%p"><Keyandroid:codes="113"android:keyLabel="q"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="119"android:keyLabel="w"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="101"android:keyLabel="e"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="114"android:keyLabel="r"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="116"android:keyLabel="t"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="121"android:keyLabel="y"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="117"android:keyLabel="y"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="105"android:keyLabel="i"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="111"android:keyLabel="o"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="112"android:keyLabel="p"android:horizontalGap="1.81%p"></Key></Row><Row android:verticalGap="1%p"><Keyandroid:codes="97"android:keyLabel="a"android:keyWidth="9%p"android:horizontalGap="5.5%p"></Key><Keyandroid:codes="115"android:keyLabel="s"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="100"android:keyLabel="d"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="102"android:keyLabel="f"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="103"android:keyLabel="g"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="104"android:keyLabel="h"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="106"android:keyLabel="j"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="107"android:keyLabel="k"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="108"android:keyLabel="l"android:keyWidth="9%p"android:horizontalGap="1%p"></Key></Row><Row android:verticalGap="1%p"><Keyandroid:codes="-1"android:keyLabel="大寫"android:keyWidth="17%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="122"android:keyLabel="z"android:horizontalGap="1%p"></Key><Keyandroid:codes="120"android:keyLabel="x"android:horizontalGap="1%p"></Key><Keyandroid:codes="99"android:keyLabel="c"android:horizontalGap="1%p"></Key><Keyandroid:codes="118"android:keyLabel="v"android:horizontalGap="1%p"></Key><Keyandroid:codes="98"android:keyLabel="b"android:horizontalGap="1%p"></Key><Keyandroid:codes="110"android:keyLabel="n"android:horizontalGap="1%p"></Key><Keyandroid:codes="109"android:keyLabel="m"android:horizontalGap="1%p"></Key><Keyandroid:codes="-5"android:isRepeatable="true"android:keyWidth="17%p"android:horizontalGap="1%p"></Key></Row><Row><Keyandroid:codes="-2"android:keyLabel="123"android:keyWidth="20%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="32"android:keyLabel="space"android:keyWidth="48%p"android:horizontalGap="5%p"></Key><Keyandroid:codes="-4"android:keyLabel="完成"android:keyWidth="20%p"android:horizontalGap="5%p"></Key></Row> </Keyboard>創建keyboard_letter.xml文件(字母鍵盤布局)
<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:keyWidth="10%p"android:keyHeight="7%p"android:horizontalGap="0.0px"android:verticalGap="0.0px"><Row android:verticalGap="1%p"><Keyandroid:codes="113"android:keyLabel="q"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="119"android:keyLabel="w"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="101"android:keyLabel="e"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="114"android:keyLabel="r"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="116"android:keyLabel="t"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="121"android:keyLabel="y"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="117"android:keyLabel="y"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="105"android:keyLabel="i"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="111"android:keyLabel="o"android:horizontalGap="1.81%p"></Key><Keyandroid:codes="112"android:keyLabel="p"android:horizontalGap="1.81%p"></Key></Row><Row android:verticalGap="1%p"><Keyandroid:codes="97"android:keyLabel="a"android:keyWidth="9%p"android:horizontalGap="5.5%p"></Key><Keyandroid:codes="115"android:keyLabel="s"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="100"android:keyLabel="d"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="102"android:keyLabel="f"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="103"android:keyLabel="g"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="104"android:keyLabel="h"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="106"android:keyLabel="j"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="107"android:keyLabel="k"android:keyWidth="9%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="108"android:keyLabel="l"android:keyWidth="9%p"android:horizontalGap="1%p"></Key></Row><Row android:verticalGap="1%p"><Keyandroid:codes="-1"android:keyLabel="大寫"android:keyWidth="17%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="122"android:keyLabel="z"android:horizontalGap="1%p"></Key><Keyandroid:codes="120"android:keyLabel="x"android:horizontalGap="1%p"></Key><Keyandroid:codes="99"android:keyLabel="c"android:horizontalGap="1%p"></Key><Keyandroid:codes="118"android:keyLabel="v"android:horizontalGap="1%p"></Key><Keyandroid:codes="98"android:keyLabel="b"android:horizontalGap="1%p"></Key><Keyandroid:codes="110"android:keyLabel="n"android:horizontalGap="1%p"></Key><Keyandroid:codes="109"android:keyLabel="m"android:horizontalGap="1%p"></Key><Keyandroid:codes="-5"android:isRepeatable="true"android:keyWidth="17%p"android:horizontalGap="1%p"></Key></Row><Row><Keyandroid:codes="-2"android:keyLabel="123"android:keyWidth="20%p"android:horizontalGap="1%p"></Key><Keyandroid:codes="32"android:keyLabel="space"android:keyWidth="48%p"android:horizontalGap="5%p"></Key><Keyandroid:codes="-4"android:keyLabel="完成"android:keyWidth="20%p"android:horizontalGap="5%p"></Key></Row> </Keyboard>《二》創建IKeyboardView類并繼承KeyboardView類,設置鍵盤布局(數字和字母)
public class IKeyBoardView extends KeyboardView implements KeyboardView.OnKeyboardActionListener {public IIKeyBoardView(Context context, AttributeSet attrs) {super(context, attrs);init();}public IKeyBoardView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}public IIKeyBoardView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);init();}/*** 數字鍵盤*/private Keyboard keyboardNumber;/*** 字母鍵盤*/private Keyboard keyboardLetter;//綁定的輸入框private EditText mEditText;/*** 是否發生鍵盤切換*/private boolean changeLetter = false;/*** 是否為大寫*/private boolean isCapital = false;private List<Integer> noLists = new ArrayList<>();private int[] arrays = new int[]{Keyboard.KEYCODE_SHIFT, Keyboard.KEYCODE_MODE_CHANGE,Keyboard.KEYCODE_CANCEL, Keyboard.KEYCODE_DONE, Keyboard.KEYCODE_DELETE,Keyboard.KEYCODE_ALT, 32};//初始化private void init(){keyboardNumber = new Keyboard(getContext(), R.xml.keyboard_num);keyboardLetter = new Keyboard(getContext(), R.xml.keyboard_letter);//設置一些不需要預覽的鍵位for (int i = 0; i < arrays.length; i++) {noLists.add(arrays[i]);}//默認使用數字鍵盤setKeyboard(keyboardNumber);//是否啟用預覽setPreviewEnabled(true);//鍵盤動作監聽setOnKeyboardActionListener(this);}@Overridepublic void onPress(int primaryCode) {}@Overridepublic void onRelease(int primaryCode) {}@Overridepublic void onKey(int primaryCode, int[] keyCodes) {}@Overridepublic void onText(CharSequence text) {}@Overridepublic void swipeLeft() {}@Overridepublic void swipeRight() {}@Overridepublic void swipeDown() {}@Overridepublic void swipeUp() {} }《三》 處理自定義鍵盤按鍵的點擊事件以及預覽,并實現數字和字母鍵盤(包括大小寫)之間的切換
核心代碼onKey方法(設置鍵盤輸入)
//在onKey回調中的代碼 Editable editable = mEditText.getText();int start = mEditText.getSelectionStart();switch (primaryCode) {case Keyboard.KEYCODE_DELETE://刪除if (editable != null && editable.length() > 0 && start > 0) {editable.delete(start - 1, start);}break;case Keyboard.KEYCODE_MODE_CHANGE://字母鍵盤與數字鍵盤切換changeKeyBoard(!changeLetter);break;case Keyboard.KEYCODE_DONE://完成changeKeyBoard(!changeLetter);break;case Keyboard.KEYCODE_SHIFT://大小寫切換changeCapital(!isCapital);setKeyboard(keyboardLetter);break;default:editable.insert(start, Character.toString((char) primaryCode));break;}還有幾個必要的方法附上
/*** 切換鍵盤大小寫*/private void changeCapital(boolean b) {isCapital = b;List<Keyboard.Key> lists = keyboardLetter.getKeys();for (Keyboard.Key key : lists) {if (key.label != null && isKey(key.label.toString())) {if (isCapital) {key.label = key.label.toString().toUpperCase();key.codes[0] = key.codes[0] - 32;} else {key.label = key.label.toString().toLowerCase();key.codes[0] = key.codes[0] + 32;}} else if (key.label != null && key.label.toString().equals("小寫")) {key.label = "大寫";} else if (key.label != null && key.label.toString().equals("大寫")) {key.label = "小寫";}}}/*** 判斷此key是否正確,且存在 * * @param key * @return*/private boolean isKey(String key) {String lowercase = "abcdefghijklmnopqrstuvwxyz";if (lowercase.indexOf(key.toLowerCase()) > -1) {return true;}return false;}/*** 切換鍵盤類型*/private void changeKeyBoard(boolean b) {changeLetter = b;if (b) {setKeyboard(keyboardLetter);} else {setKeyboard(keyboardNumber);}}/*** 判斷是否需要預覽Key** @param primaryCode keyCode*/private void canShowPreview(int primaryCode) {if (noLists.contains(primaryCode)) {setPreviewEnabled(false);} else {setPreviewEnabled(true);}}//設置需要綁定的EditViewpublic void setmEditText(EditText mEditText) {this.mEditText = mEditText;}《四》 綁定EditText,并且屏蔽系統鍵盤(實現點擊切換綁定多個EditText)
這塊我屏蔽鍵盤的方式是比較粗暴的,直接在EditTextView當中設置了屏蔽焦點,這樣的同時會讓EditTextView丟失光標
public class IEditText extends AppCompatEditText {private IKeyboardView mIKeyboardView;public IEditText(@NonNull Context context) {super(context);init();}public IEditText(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs);init();}public IEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}private void init() {setInputType(InputType.TYPE_NULL);}//綁定鍵盤public void setmIKeyboardView(IKeyboardView mIKeyboardView) {this.mIKeyboardView = mIKeyboardView;}@Overridepublic boolean onTouchEvent(MotionEvent event) {//按下時綁定當前的EditTextif (event.getAction() == MotionEvent.ACTION_DOWN){if (mIKeyboardView!=null){mIKeyboardView.setEditText(this);}}return super.onTouchEvent(event);}}《五》. 使用
例如:兩個EditTextView一個自定義鍵盤,因為太簡單我就不放出布局代碼了(占位置) 就看看咱是如何綁定的
public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity:";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);//默認綁定一個EditTextViewbinding.viewKeyboard.setEditText(binding.ed1);//點擊時切換綁定鍵盤binding.ed1.setmIKeyboardView(binding.viewKeyboard);binding.ed2.setmIKeyboardView(binding.viewKeyboard);}}4.KeyboardView,keyboard,Key,Row屬性解釋:
- Keyboard
- Row
- Key
- KeyboardView
中,
activity標簽下面不要使用該屬性
android:hardwareAccelerated=“true”
不然會導致你鍵盤卡頓以及點擊事件錯位問題
6.結尾
如果大家覺得有哪里寫得有問題,可以告訴我,一起學習
參考鏈接
總結
以上是生活随笔為你收集整理的Android自定义键盘(KeyboardView)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: db4o_8.0对象数据库官方文档翻译_
- 下一篇: 计算机毕业设计ssm+vue基本微信小程