自定義軟鍵盤
1.繼承KeyboardView
并實現(xiàn)KeyboardView.OnKeyboardActionListener
接口
繼承KeyboardView
中最重要的就是設(shè)置鍵盤布局:
//設(shè)置軟鍵盤布局
Keyboard keyboard = new Keyboard(context, R.xml.keyboard_sn_input); setKeyboard(keyboard);
鍵盤布局了解一下:
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="33.333333%p"
android:keyHeight="8%p"
android:horizontalGap="1dp"
android:verticalGap="1dp">
<Row>
<Key
android:isRepeatable="true"
android:codes="65"
android:keyLabel="A"/>
...
</Row>
...
<Row>
<Key
android:codes="-10"
android:keyIcon="@drawable/ic_delete_forever"/>
...
<Key
android:keyIcon="@drawable/ic_arrow_back"
android:isRepeatable="true"
android:codes="-5" />
</Row>
</Keyboard>
簡單易懂鲜棠,其中注意幾個屬性:
-
android:isRepeatable
:表示這個按鍵是否可以長按重復(fù)輸入; -
android:codes
:表示這個按鍵的輸入內(nèi)容卵凑; -
android:keyLabel
:表示這個按鍵的顯示值拓提; -
android:keyIcon
:表示這個按鍵的顯示圖片;
對于一般輸入如"A-65"按鍵可以直接獲取輸入值仿畸,那么如果是圖片按鍵志衍,那么直接獲取其輸入值然后再設(shè)置對應(yīng)的處理事件即可萍聊,如上文中的:
<Key
android:keyIcon="@drawable/ic_arrow_back"
android:isRepeatable="true"
android:codes="-5" />
實現(xiàn)KeyboardView.OnKeyboardActionListener
接口:
-
void onPress(int primaryCode)
:按鈕按下時的反饋值崭倘,也就是我們設(shè)置的android:codes
翼岁; -
void onRelease(int primaryCode)
:按鈕松開時的反饋值; -
void onKey(int primaryCode, int[] keyCodes)
:一次點擊的回調(diào)司光,其中如果是單次點擊琅坡,那么primaryCode
即為反饋值; -
void onText(CharSequence text)
:如果如下在xml中對按鍵設(shè)置android:keyOutputText
屬性残家,那么點擊反饋就是一個String榆俺,由此回調(diào);
<Key
android:keyOutputText="hello world!"
android:codes="-10"
android:keyIcon="@drawable/ic_delete_forever"/>
-
void swipeLeft()
:左劃觸發(fā); -
void swipeRight()
:右劃觸發(fā)谴仙; -
void swipeDown()
:下劃觸發(fā)迂求; -
void swipeUp()
:上劃觸發(fā);
2.監(jiān)聽按鍵回調(diào)
簡單的demo我們只需要通過void onKey(int primaryCode, int[] keyCodes)
監(jiān)聽按鍵回調(diào):
同時為了能夠在外部設(shè)置處理事件晃跺,新建一個回調(diào)接口:
public interface IOnKeyboardListener {
void onInsertKeyEvent(String text);
void onDeleteKeyEvent();
void onClearKeyEvent();
}
private IOnKeyboardListener onKeyboardListener;
//setter
public void setOnKeyboardListener(IOnKeyboardListener onKeyboardListener) {
this.onKeyboardListener = onKeyboardListener;
}
監(jiān)聽部分:
//用于區(qū)分左下角空白按鍵
private static final int KEYCODE_CLEAR = -10;
private static final int KEYCODE_DELETE = -5;
@Override
public void onKey(int primaryCode, int[] keyCodes) {
//處理按鍵的點擊事件
//點擊刪除按鍵
if (primaryCode == Keyboard.KEYCODE_DELETE) {
if (onKeyboardListener != null) {
onKeyboardListener.onDeleteKeyEvent();
}
} else if (primaryCode == KEYCODE_CLEAR) {
if (onKeyboardListener != null) {
onKeyboardListener.onClearKeyEvent();
}
} else {
if (onKeyboardListener != null) {
onKeyboardListener.onInsertKeyEvent(Character.toString((char) primaryCode));
}
}
}
3.鍵盤的一些其他方法
//是否顯示按鍵選擇預(yù)覽
setPreviewEnabled(false);
這個方法如果設(shè)置為true就需要在KeyBoardView
布局文件中中添加屬性:
android:keyPreviewLayout="@layout/view_preview"
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white_background">
</TextView>
一定不能缺少android:background
4.對應(yīng)的EditText的處理
既然自定義了鍵盤,那么就必須隱藏原有的鍵盤毫玖,同時也不能把光標隱藏掉掀虎,處理如下,低版本不考慮了:
try{
Class<EditText> cls=EditText.class;
Method setShowSoftInputOnFocus;
setShowSoftInputOnFocus=cls.getMethod("setShowSoftInputOnFocus",boolean.class);
setShowSoftInputOnFocus.setAccessible(true);
setShowSoftInputOnFocus.invoke(snEditText,false);
}catch (Exception e){
e.printStackTrace();
}