我有一个包含大约10个控件(文本和TextView
)的AlertDialog
对话框,这些控件都在带有ScrollView
的AlertDialog
中,并且还有2个按钮(积极和消极)。问题是当软键盘弹出时,两个按钮被键盘遮挡了。
我正在寻找一种像是在我的内部视图或对话框上重新绘制的功能。下面是我所说内容的屏幕截图。
我有一个包含大约10个控件(文本和TextView
)的AlertDialog
对话框,这些控件都在带有ScrollView
的AlertDialog
中,并且还有2个按钮(积极和消极)。问题是当软键盘弹出时,两个按钮被键盘遮挡了。
我正在寻找一种像是在我的内部视图或对话框上重新绘制的功能。下面是我所说内容的屏幕截图。
如果您的对话框是使用对话框主题之一的活动,则可以通过为活动的windowSoftInputMode
参数设置adjustResize
标志来实现此行为。
我正在使用:
android:windowSoftInputMode="adjustResize|stateHidden"
我认为你仍然可以在常规对话框中使用此标志,但是我不确定如何应用它。您可能需要使用自定义主题创建AlertDialog,该主题继承正确的父主题并设置该标志,或者您可能需要使用ContextThemeWrappers等工具。
或者您可以直接使用Window#setSoftInputMode。
alertDialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
我发现了一个最佳处理方法。由于这是一个对话,因此代码
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
不太有效。
除了这段代码,你还必须为此对话框设置一个对话框样式。样式应该像下面这样:
<style name="DialogStyle" parent="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<item name="android:windowFullscreen">false</item>
......
......
</style>
注意,属性parent
像活动的样式一样设置为Theme.Black.NoTitleBar.Fullscreen
,而属性android:windowFullScreen
应该是false
。
现在,当软键盘切换时,对话框将会被重新调整大小。
parent="Theme.AppCompat.Light.Dialog.Alert"
,但将 "android:windowFullscreen"
设置为 false 确实起到了作用...谢谢。 - CrandellWS除了 adjustPan
之外,其他的都没有用。
根据文档说明:
活动的主窗口不会被调整大小以腾出空间给软键盘。相反,窗口的内容会自动滚动,以便当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在输入什么。这通常比调整大小不太理想,因为用户可能需要关闭软键盘才能访问并与窗口的被遮挡部分进行交互。
因此,在您的 onCreate() 或 onCreateView() 方法中,只需简单地使用它即可:
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
或者在我们处理对话框的活动中,只需在清单中放置android:windowSoftInputMode="adjustPan"
,并在每个编辑框中使用android:windowSoftInputMode="adjustResize|stateHidden"
,这将帮助用户轻松地导航到下一个文本框。
需要注意的点:
永远不要使用MATCH_PARENT来使对话框全屏,因为在这里adjustPan
不起作用。如果有人想要让对话框适应屏幕,请使用高度为0.96的点(不要超过此值),这样键盘就可以正确地到达编辑框。我像下面这样做:
@Override
public void onStart()
{
super.onStart();
Dialog dialog = getDialog();
if (dialog != null)
{
//int height = ViewGroup.LayoutParams.MATCH_PARENT;
int width = ViewGroup.LayoutParams.MATCH_PARENT;
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
//int width = (int)(size.x * 0.96);
int h = (int)(size.y * 0.96);
dialog.getWindow().setLayout(width, h);
}
}
如果我使用总高度(MATCH_PARENT),那么软键盘会挤压对话框。但是,如果我使用高度点数(这里是0.96,几乎接近于MATCH_PARENT),则它将正常工作。
希望能帮到某些人 :)
立即显示键盘并调整大小:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
}
});
<style name="SomeStyleName">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
windowTranslucentStatus
和windowTranslucentNavigation
都为true,键盘会覆盖对话框。<style name="SomeStyleName">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="materialAlertDialogTheme">@style/TranslucentMaterialAlertDialogTheme</item>
</style>
<style name="TranslucentMaterialAlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>