如何在键盘上方显示对话框

8

我是一名android新手,我编写了一个应用程序,使用对话框来显示用户在选择某个选项时的数据。这是对话框的外观:

https://docs.google.com/file/d/0B3NUAgD0tB0YOS16azFCWXdSVVE/edit

但是当我点击最后一个EditText以输入一些数据时,对话框仍然显示着,在我输入第一个字符时,对话框会向下滚动。对话框停留在键盘后面,有些部分完全被遮挡住了。

请问有人能告诉我如何将整个对话框显示在软键盘上方吗?这就是我希望它看起来的样子:

https://docs.google.com/file/d/0B3NUAgD0tB0YOFVQYUF0U0JvOEk/edit

谢谢

Clark

4个回答

30

非常好,这就是我想要的。我可以在软键盘上方显示对话框。 - user1597721

9

为了使软输入模式起作用,您可能需要手动设置对话框的宽度和高度,如下所示:

    WindowManager.LayoutParams params = window.getAttributes();
    params.width = WindowManager.LayoutParams.MATCH_PARENT;
    params.height = WindowManager.LayoutParams.MATCH_PARENT;
    params.gravity = Gravity.CENTER;
    window.setAttributes(params); 
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE ); 

1
这段代码的最后一行完全是错误的!从Android文档(https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#SOFT_INPUT_ADJUST_PAN)中了解到,`SOFT_INPUT_ADJUST_PAN`不能与`SOFT_INPUT_ADJUST_RESIZE`组合使用; - Stanislav
看起来你是对的。我之前没有查看文档,因为互联网上有很多像我这样写的例子。我现在正在修正我的回答。谢谢你催促我。 - Oguz Ozcan
如果我在AlertDialog中使用EditText,那么是否可能在其他应用程序上打开对话框上的软键盘? - Faizal Abbas

2

创建一个名为style.xml的Xml文件。

<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowSoftInputMode">stateUnchanged</item>
        <item name="android:windowBackground">@color/dialog_transparent</item>
</style>

那么实施吧

这对我有效,希望它也适用于你。

final Dialog dialog = new Dialog(this , R.style.FullHeightDialog);

同时还需要更改您的清单文件。

android:windowSoftInputMode="adjustResize|adjustPan" 

3
“adjustPan”不能与“adjustResize”组合使用。这在文档中明确说明。 - 4emodan
实际上,adjustPan | adjustResize = adjustNothing。 - user9599745

1
 AlertDialog dialog = new AlertDialog.Builder(this).create();
 dialog.show();
 Window window = dialog.getWindow();
 window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
 window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

我不想使用AlertDialog,因为我需要正确控制键盘如何弹出以适当地编辑文本。 - user9599745

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接