美文网首页高级UI
自定义软键盘

自定义软键盘

作者: Ugly_K | 来源:发表于2019-05-20 10:57 被阅读32次

自定义软键盘

1.继承KeyboardView并实现KeyboardView.OnKeyboardActionListener接口

继承KeyboardView中最重要的就是设置键盘布局:

//设置软键盘布局   
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:表示这个按键是否可以长按重复输入;
  • android:codes:表示这个按键的输入内容;
  • android:keyLabel:表示这个按键的显示值;
  • android:keyIcon:表示这个按键的显示图片;

对于一般输入如"A-65"按键可以直接获取输入值,那么如果是图片按键,那么直接获取其输入值然后再设置对应的处理事件即可,如上文中的:

 <Key
  android:keyIcon="@drawable/ic_arrow_back"
  android:isRepeatable="true"
  android:codes="-5" />

实现KeyboardView.OnKeyboardActionListener接口:

  • void onPress(int primaryCode):按钮按下时的反馈值,也就是我们设置的android:codes
  • void onRelease(int primaryCode):按钮松开时的反馈值;
  • void onKey(int primaryCode, int[] keyCodes):一次点击的回调,其中如果是单次点击,那么primaryCode即为反馈值;
  • void onText(CharSequence text):如果如下在xml中对按键设置android:keyOutputText属性,那么点击反馈就是一个String,由此回调;
 <Key
  android:keyOutputText="hello world!"
  android:codes="-10"
  android:keyIcon="@drawable/ic_delete_forever"/>
  • void swipeLeft():左划触发;
  • void swipeRight():右划触发;
  • void swipeDown():下划触发;
  • void swipeUp():上划触发;

2.监听按键回调

简单的demo我们只需要通过void onKey(int primaryCode, int[] keyCodes)监听按键回调:

同时为了能够在外部设置处理事件,新建一个回调接口:


public interface IOnKeyboardListener {
    void onInsertKeyEvent(String text);   
    void onDeleteKeyEvent();   
    void onClearKeyEvent(); 
}

private IOnKeyboardListener onKeyboardListener;

//setter
public void setOnKeyboardListener(IOnKeyboardListener onKeyboardListener) {
    this.onKeyboardListener = onKeyboardListener; 
}

监听部分:

//用于区分左下角空白按键 
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.键盘的一些其他方法

//是否显示按键选择预览
setPreviewEnabled(false);

这个方法如果设置为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.对应的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(); 
}

相关文章

网友评论

    本文标题:自定义软键盘

    本文链接:https://www.haomeiwen.com/subject/gjihzqtx.html