【问题标题】:how to set action listener to popup keyboard in my custom keyboard...如何在我的自定义键盘中将动作侦听器设置为弹出键盘...
【发布时间】:2018-12-03 20:54:39
【问题描述】:

我的键码连续

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

当长按“1”时,它会显示一个带有“w”和“W”的弹出窗口以及一个十字按钮。但是这些角色没有添加任何动作监听器。如果我单击“w”,则没有任何反应,但十字按钮有效。但是我怎样才能为这个角色添加动作监听器。在此先感谢:)

【问题讨论】:

  • 您的代码就是您的价值。它会自动将此添加到键盘的 inputMethod 中。如果您想对该键执行不同的操作,请更改代码。
  • 对不起兄弟,我不明白你试图解释什么。请你能再解释一下吗? @Nepster

标签: android android-softkeyboard


【解决方案1】:
【解决方案2】:

我的解决方案: 创建时:

 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;
        }
    }
}

【讨论】:

  • 感谢您的回答。希望它会帮助其他人。
猜你喜欢
  • 2016-02-03
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
  • 2014-11-12
  • 2015-11-10
  • 1970-01-01
相关资源
最近更新 更多