android - 在没有输入区域的 Activity 上显示数字键盘

标签 android numeric android-softkeyboard

我使用下面的代码在我的Activity中弹出软输入键盘


InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.getInputMethodList();

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

这会显示字母键盘。

但是我想显示数字键盘。

请注意,我知道在与 Edittext 或 TextView 一起使用时使用 setInputType() 是有效的,但我希望能够在没有输入区域(例如 EditText)的情况下显示键盘,并只听键盘上的按键。

谁能证实这是否可行,如果可行,如何实现?

最佳答案

这确实是可以实现和可能的。

长话短说:

覆盖 onCreateInputConnection 并请求数字键盘:

public class CustomView extends View {

    [your methods here]

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        InputConnection connection =  super.onCreateInputConnection(outAttrs);
        outAttrs.inputType |= InputType.TYPE_CLASS_NUMBER;
        return connection;
    }
}

完整解释

您已将 View 子类化以创建您自己的自定义 View :

public class CustomView extends View {
    [your methods here]
}

在此自定义 View 中,您使用的是 InputMethodManager显示键盘。这位经理将把输入委托(delegate)给 InputConnection ,然后将委托(delegate)给 InputMethod .

要请求他们的键盘显示为数字,您需要覆盖 onCreateInputConnection返回数字键盘。你可以不厌其烦地实现你自己的 BaseInputConnection ,或者您可以重用 super(在本例中为 View)为您创建的那个,并设置您关心的属性(inputType ).

关于android - 在没有输入区域的 Activity 上显示数字键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3980510/

相关文章:

android - 为适配器实现 bindView() 时,我可以 'skip' View 吗?

int - 将字母数字转换为数字并且应该是唯一数字

android - 允许在 Android 上使用漂亮的 3X3 数字键盘输入负小数

algorithm - 按关系对任何事物进行排序

Android - 如何在键盘可见性发生变化时仅调整某些 View 的大小

android - 当软键盘在 Android 中出现在 View 前面时布局应该上升

android - 如何在android中以编程方式隐藏三星galaxy note的键盘

java - 将 appcompat-v7 和设计库更新到 v24.2.0 会导致错误

android - 依赖图构建是在 Dagger 2 的编译时还是运行时发生的

android - 当我在打开的简历中传递newSize参数时,resize()失败