安卓输入法 : how to show a pop-up dialog?

标签 android keyboard ime

我正在玩一些键盘开发,并尝试在按下某个键时显示弹出对话框

if (primaryCode == -301) {
            AlertDialog mDialog = new AlertDialog.Builder(CONTEXT)
            .setTitle("My dialog")
            .setMessage("Lets do it.")
            .setPositiveButton("ok", null).create();
             mDialog.show();
}

但是,问题出在 CONTEXT 部分。在一个普通的应用程序中,它只是this。我还尝试了 getApplicationContext()getBaseContext(),但它们都不起作用 -> 键盘崩溃。

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

所以我想知道我是否必须对 InputConnection 做些什么:

The InputConnection interface is the communication channel from an InputMethod back to the application that is receiving its input. It is used to perform such things as reading text around the cursor, committing text to the text box, and sending raw key events to the application.

到目前为止,我无法弄清楚该怎么做。我绝对知道这是可能的,因为我以前见过它。有人可以为我指明正确的方向,我将不胜感激。


更新:

为了更好地展示我试图实现的目标,我上传了一张 Swype 键盘的屏幕截图,它的作用就是:在键盘上按下特殊键时显示弹出对话框。

Swype pop-up dialog

最佳答案

愿那些遵从指引的人平安,

解决方案:

AlertDialog dialog;
//add this to your code
       dialog = builder.create();
        Window window = dialog.getWindow(); 
        WindowManager.LayoutParams lp = window.getAttributes();
            lp.token = mInputView.getWindowToken();
            lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
            window.setAttributes(lp);
            window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//end addons
alert.show();

祝你好运。

关于安卓输入法 : how to show a pop-up dialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3494476/

相关文章:

android - 如何从 Fragments 以编程方式设置 windowsoftinputmode?

android - 创建自定义号码选择器

keyboard - 在键盘隐藏的 flutter 页面文本字段中

android - 保存数据的最佳方法 - 首选项、sqlite、可序列化或其他?

keyboard - 模拟键盘的输出(使计算机成为另一台计算机的键盘)

android - 暂停或停止 fragment 后未显示键盘

android - Android Studio 模拟器中的 IME 不响应键盘上的 Shift-B

java - 如何处理 Android 键盘 (IME) 中的配置更改?

java - 使用 Json 解析 Gson 不适用于 Android

Android RecyclerView 不会更新数据更改