我正在玩一些键盘开发,并在按下某个键时尝试显示弹出对话框
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:无法添加窗口-令牌null为不适用于应用程序
所以我想知道我是否必须对InputConnection做点什么:
InputConnection接口是通讯渠道InputMethod回到应用程序接收到它的输入。它是用来执行诸如阅读之类的事情光标周围的文本,提交文本到文本框,然后发送原始应用程序的关键事件。
到目前为止,我还不知道如何。我肯定知道这是可能的,因为我之前已经看过。我可以向我指出正确的方向,这将是不胜感激的。
更新:
[为了提供我想要达到的目标的更好的图片,我上传了Swype键盘的屏幕截图,该截图恰好做到了:在键盘上按下特殊键时显示一个弹出对话框。
“ >>
我正在尝试一些键盘开发,如果(primaryCode == -301){AlertDialog mDialog = new AlertDialog.Builder(...
祝愿遵循指导的人,>>
解决方案:
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();
IME不在应用程序上下文中运行,因此您不能使用普通的应用程序窗口。您可以使用对话框,但需要修改其窗口,以使窗口类型为以下类型:
进行一项活动,并在清单文件中为该活动赋予属性
android:theme="@android:style/Theme.Dialog"
非常简单,只需在此处创建活动(没有任何视图),并在其中编写对话框代码
public class dialog extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Delete All Contacts?");
builder.setCancelable(false);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//deleteAllContacts();
}//
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
您必须获得对您的活动上下文的引用。无论如何,您应该使用Activity的showDialog方法。
// 1. CREATE THE DIALOG
val builder: AlertDialog.Builder = AlertDialog.Builder(this, R.style.Theme_AppCompat_Light)
builder.setTitle("Title").setMessage("This is the message for the user. ")
val mDialog = builder.create()
// 2. SET THE IME WINDOW TOKEN ATTRIBUTE WITH THE TOKEN OF THE KEYBOARD VIEW
mDialog.window?.attributes?.token = this.mTblView.windowToken
// 3. SET THE TYPE OF THE DIALOG TO TYPE_APPLICATION_ATTACHED_DIALOG
mDialog.window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG)
// 4. SHOW THE DIALOG
mDialog.show()