如何在我的自定义键盘中设置动作监听器弹出键盘...

时间:2015-02-01 11:08:01

标签: android android-softkeyboard

我的密钥代码连续

<Key android:codes="49" android:keyLabel="1"  android:verticalGap="2%p"  android:popupKeyboard="@xml/popupview" android:popupCharacters="wW" android:keyEdgeFlags="left"/>

它会显示一个带有'w'和'W'的弹出窗口,以及长按'1'时的十字按钮。但是没有动作监听器添加到这些角色。如果我点击'w'没有任何事情发生,但十字按钮工作。但是我怎样才能为这个角色添加动作监听器。提前谢谢:)

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

My Solution: OnCreate:

 mKeyboardView.setOnKeyboardActionListener(new ActionListener(
            Activity.this,edittext, mKeyboardView));

 public class BasicOnKeyboardActionListener implements KeyboardView.OnKeyboardActionListener {

    EditText editText;
    CustomKeyboardView displayKeyboardView;
    private Activity mTargetActivity;

    public ActionListener(Activity targetActivity, EditText editText,
                                         CustomKeyboardView
                                                 displayKeyboardView) {
        mTargetActivity = targetActivity;
        this.editText = editText;
        this.displayKeyboardView = displayKeyboardView;
    }
      @Override
      public void onText(CharSequence text) {
        int cursorPosition = editText.getSelectionEnd();
        String previousText = editText.getText().toString();
        String before, after;
        if (cursorPosition < previousText.length()) {
            before = previousText.substring(0, cursorPosition);
            after = previousText.substring(cursorPosition);
        } else {
            before = previousText;
            after = "";
        }
        editText.setText(before + text + after);
        editText.setSelection(cursorPosition + 1);
       }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        switch (primaryCode) {
            case xx:// xx is primaryCode that is given program for pressed key
                onText("C");// C is char what you want to write
            default:                   
            break;
        }
    }
}