【问题标题】:What is the best way of managing keyboard input in Android NativeActivity?在 Android NativeActivity 中管理键盘输入的最佳方法是什么?
【发布时间】:2014-05-29 16:16:59
【问题描述】:

我在纯本机应用程序(基于 Android NDK 中提供的native-activity sample)应用程序中使用键盘时遇到问题。

我有这个 Java 代码来显示键盘:

mApplicationActivity.runOnUiThread(new Runnable(){
    @Override
    public void run() {

        if(mTextEdit != null)
            mTextEdit.setVisibility(View.GONE);     
        mTextEdit = new EditText(mApplicationActivity);

        InputMethodManager m = (InputMethodManager) mApplicationActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
        mTextEdit.setText(mTextEditValue);
        mEditTextLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        mEditTextLayoutParams.gravity = Gravity.TOP;
        mEditTextLayoutParams.setMargins(left, top, right, bottom);
        mTextEdit.setLayoutParams(mEditTextLayoutParams);
        mTextEdit.setVisibility(View.VISIBLE);
        mTextEdit.addTextChangedListener(mTextWatcher);

        mApplicationActivity.addContentView(mTextEdit, mEditTextLayoutParams);

        mTextEdit.bringToFront();
        mTextEdit.setSelection(mTextEdit.getText().length());
        mTextEdit.requestFocus();

        m.showSoftInput(mTextEdit, InputMethodManager.SHOW_FORCED);
    }
});

我使用 textWatcher 检测输入更改以通知我的 C++ 代码并更新 OpenGL UI。它似乎在我的 Nexus 4 和 Android 4.4.2 中正常工作,但今天我有一个 Android 4.2.2 的 Nexus 4,我无法删除我写的文字。

所以我想知道处理键盘输入的最佳方法是什么:

  • 我使用 OpenGL 渲染自己的 UI,EditText 在屏幕之外。
  • 使用按键事件是个坏主意(如在此解释:https://code.google.com/p/android/issues/detail?id=42904“它无法正常使用手势输入、语音输入或切换输入,或者开发人员可能想出的任何新的输入法未来。”)

我也会考虑是否可以将 Java EditText 放在我的 OpenGL 上,但我不知道该怎么做。

这是 textWatcher 代码:

TextWatcher mTextWatcher = new TextWatcher() {  
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Call to C++ code 
            SendChangedText(s.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

【问题讨论】:

  • textwathcer 是要走的路,你能显示 mtextwatcher 的代码吗??
  • 是的,我已将其添加到问题中。它没有什么特别的,只是调用我的 C++ 代码来更新 UI。调试我注意到在 Android 4.2.2 上按退格键时根本不会调用 textwatcher 事件。

标签: android android-ndk keyboard


【解决方案1】:

我还在我的项目中使用 NativeActivity。关键事件方法 -

public boolean dispatchKeyEvent(KeyEvent event) { ... }

只在开始时为我工作,直到有一天,在 4.4 下,存在退格(删除)键事件根本没有发送到事件处理程序的错误。似乎它已知的基本 android 键盘错误。所以我从原始按键处理转移到“隐藏的 EditText”解决方案。

Hidden EditText 让您可以摆脱错误并处理预测输入。为此,您需要实现自己的 InputConnectionWrapper(或者它也可以是 BaseInputConnection)。在 EditText 内部覆盖 onCreateInputConnection:

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {

    // We set manual input connection to catch all keyboard interaction
    return new MyInputConnection(super.onCreateInputConnection(outAttrs), true);

}

@Override
public boolean onCheckIsTextEditor() {
    return true;
}

现在我们可以在里面捕获很多输入事件:

class MyInputConnection extends InputConnectionWrapper {

public MyInputConnection(InputConnection target, boolean mutable) {
    super(target, mutable);
}

@Override
public boolean setComposingText(CharSequence text, int newCursorPosition)
{
    Log.d(TAG, "setComposingText " + text);
    return super.setComposingText(text, newCursorPosition);
}

@Override
public boolean commitText(CharSequence text, int newCursorPosition)
{
    Log.d(TAG, "commitText " + text);
    return super.commitText(text, newCursorPosition);
}

@Override
public boolean sendKeyEvent(KeyEvent event) {
    return super.sendKeyEvent(event);
}


@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {       
    if (beforeLength == 1 && afterLength == 0) {
            Log.d(TAG, "DELETE!");
    }

    return super.deleteSurroundingText(beforeLength, afterLength);
}

}

有关详细信息,请参阅 InputConnectionWrapper 类。据我了解,从软件键盘捕获所有事件的唯一方法是覆盖此接口提供的所有方法。

【讨论】:

    猜你喜欢
    • 2012-11-22
    • 2012-07-26
    • 2010-10-11
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    相关资源
    最近更新 更多