我正在玩一些键盘开发,并尝试在按下某个键时显示弹出对话框
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 键盘的屏幕截图,它的作用就是:在键盘上按下特殊键时显示弹出对话框。
最佳答案
愿那些遵从指引的人平安,
解决方案:
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/