在没有 EditText 的情况下强制 Android 的软键盘处于 NumberPassword 模式

Force Android's Softkeyboard in NumberPassword mode without EditText

有什么方法可以强制在 NumberPassword 模式下显示 Android 的 SoftKeyboard 而无需在我的 activity 中使用实际的 EditText?

当 activity 开始时,我通过在我的 AndroidManifest.xml 上添加 android:windowSoftInputMode="stateAlwaysVisible" 来设法显示键盘,通过覆盖我的 [=] 中的 onKeyPreIme 使其无法关闭13=] class 扩展了 TextView,并通过覆盖我的 Activity.

中的 onKeyUp 自行处理触摸事件

如果我直接在 CustomView 的 XML 布局中添加 android:inputType="numberPassword",Activity 的 onKeyUp 将被绕过,键盘在我的 CustomView 中写入字符并且 KEYCODE_ENTER 关闭我的键盘.

我要实现的是:

为方便起见取自https://developer.android.com/training/keyboard-input/commands.html

Both the Activity and View class implement the KeyEvent.Callback interface, so you should generally override the callback methods in your extension of these classes as appropriate.

我建议您覆盖 CustomView class 中 onKeyUp 的默认实现,并使 CustomView.onKeyUp 方法将事件重定向到您的 ActivityonKeyUp方法。

举个例子:

public class CustomView extends TextView {
    private KeyEvent.Callback myKeyEventCallback;

    public void setCustomKeyEventCallback(KeyEvent.Callback callback) {
        myKeyEventCallback = callback;
    }

    ...

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return myKeyEventCallback.onKeyUp(keyCode, event);
    }
}

然后在您的 Activity 中执行此操作:

CustomView view = ...; // here you take the reference to your custom view
view.setCustomKeyEventCallback(new KeyEvent.Callback() {
    // ... other methods

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // this calls your activity's implementation of onKeyUp
        MyActivity.this.onKeyUp(keyCode, event);
        return false; // prevent event from firing twice
    }
});

这将帮助您将 onKeyUp 方法调用从 CustomView 重定向到 Activity 的 onKeyUp 实现。