Android IME:如何显示弹出对话框? [英] Android IME: how to show a pop-up dialog?

查看:179
本文介绍了Android IME:如何显示弹出对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm playing around with some keyboard development and try to show a pop-up dialog when a certain key is pressed

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(),但是这些都不起作用->键盘崩溃.

However, the problem is the CONTEXT part. In a normal application it would just be this. I also tried getApplicationContext() and getBaseContext(), but neither of those works -> keyboard crashes.

android.view.WindowManager $ BadTokenException: 无法添加窗口-令牌null为 不适用于应用程序

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

所以我想知道我是否必须对 InputConnection做些事情:

So I'm wondering if I have to do something with InputConnection:

InputConnection接口是 来自一个人的沟通渠道 InputMethod回到应用程序 接收到它的输入.它是 用来执行诸如阅读之类的事情 光标周围的文本,提交 文本到文本框,然后发送原始 应用程序的关键事件.

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.

到目前为止,我还无法弄清楚该怎么做.我绝对知道这是可能的,因为我之前已经看过它.我可以向我指出正确的方向,这将是不胜感激的.

So far I wasn't able to figure out how. I definitely know it's possible, since I have seen it before. I someone could point me in the right direction that would definitely be appreciated.

更新:

为了提供更好的画面说明,我上传了Swype键盘的屏幕截图,该截图确实做到了:当按下键盘上的特殊键时,会显示一个弹出对话框.

To provide a better picture of what I try to achieve I uploaded a screenshot of the Swype keyboard, which does exactly that: showing a pop-up dialog when a special key gets pressed on the keyboard.

推荐答案

愿遵循指南的人

解决方案:

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();

祝你好运.

这篇关于Android IME:如何显示弹出对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆