Android IME:显示自定义弹出对话框(如 Swype 键盘),可以在 TextView 中输入文本

发布于 2024-09-14 13:49:59 字数 652 浏览 3 评论 0原文

我想知道如何创建一个自定义弹出窗口,如下面的屏幕截图中的那样(借自 Swype 键盘),其中我可以有几个按钮,每个按钮都将一个字符串提交到当前“已连接”TextView(通过 InputConnection)。

请注意:这是一个InputMethodService并且不是普通的Activity。我已经尝试使用 Theme:Dialog 启动单独的 Activity 。然而,一旦打开它,我就会失去对 TextView 的焦点,并且我的键盘也会消失(随之我的 InputConnection 也消失了)。

Swype

I'm wondering how I can create a custom pop-up like the one in the screenshot below (borrowed from the Swype keyboard), where I can have a couple of buttons, which each commit a string to the currently "connected" TextView (via a InputConnection).

Please note: this is an InputMethodService and not an ordinary Activity. I already tried launching a separate Activity with Theme:Dialog. However, as soon as that one opens I lose my focus with the TextView and my keyboard disappears (and with that my InputConnection is gone).

Swype

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

睡美人的小仙女 2024-09-21 13:49:59

您可以尝试使用 PopupWindow。您必须做一些修改才能让它执行您想要的操作,而唯一好的文档就是源代码。

You can try using a PopupWindow. You'll have to do a bit of hacking to get it to do what you want and the only good documentation for it is the source.

夏雨凉 2024-09-21 13:49:59

我也曾为这个问题绞尽脑汁,最后终于解决了。上述解决方案是正确的,尽管正如您所指出的,它们不能从 InputMethodService 中使用,因为它不是 Activity。诀窍是在 KeyboardView 的子类中创建 PopupWindow 。通过使用负 Y 位置,PopupWindow 可以像 Swype 一样出现在键盘上方。

祝你好运,
巴里

I was banging my head against this problem too and I finally figured it out. The above solutions are correct although as you pointed out they cannot be used from an InputMethodService because it is not an Activity. The trick is to create the PopupWindow in a subclass of KeyboardView. By using a negative Y position, the PopupWindow can appear above the keyboard like Swype.

Good luck,
Barry

沧笙踏歌 2024-09-21 13:49:59

正确答案:

  1. 创建一个 PopupWindow 并将您的视图放入其中
  2. 调用 popupWindow.setClippingEnabled(false)
  3. 调用 [popupWindow.showAtLocation( )](http://developer.android. com/reference/android/widget/PopupWindow.html#showAtLocation(android.view.View, int, int, int)) 带有负 Y 坐标,

这将在 IME 上方显示您的弹出窗口,如屏幕截图所示。

Correct answer:

  1. Create a PopupWindow and put your view inside it
  2. Call popupWindow.setClippingEnabled(false)
  3. Call [popupWindow.showAtLocation()](http://developer.android.com/reference/android/widget/PopupWindow.html#showAtLocation(android.view.View, int, int, int)) with a negative Y coordinate.

This will show your popup above the IME as in your screenshot.

ㄟ。诗瑗 2024-09-21 13:49:59

愿那些遵循指导的人平安,

解决方案:

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

dialog.show();

===== 更新 30.09.2015
mInputView 它是键盘类的通用名称..请参阅

@Override
    public View onCreateInputView() {
        mInputView =(MyKeyboardView) getLayoutInflater().inflate( R.layout.input, null);
....
}

更多信息: http://developer.android.com/guide/topics/text/creating-input-method.html

祝你好运。

Peace be upon those who follow the guidance,

solution :

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

dialog.show();

===== UPDATE 30.09.2015
mInputView its the general name of your keyboard class ..see

@Override
    public View onCreateInputView() {
        mInputView =(MyKeyboardView) getLayoutInflater().inflate( R.layout.input, null);
....
}

More info : http://developer.android.com/guide/topics/text/creating-input-method.html

good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文