工作中发现在AlertDialog中加入EditText后,Android 4.4 ,SDK19 或以下的手机,软键盘弹出后会遮挡Dialog中的按钮,而5.1的手机Dialog会自动向上移动一些。

首先遇到不会的先上网查查吧
网上有一些方案,
Window window = dialog.getWindow();
if (window != null) {
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
跑了一下,没效果,也许是没用对地方吧,还有一些别的方案,试了试都没效果,自己想办法吧
经过一番研究,发现直接在构造Dialog时加上Theme,在Theme里设置软键盘弹出方案 就可以了
第一步:创建theme:
在styles.xml文件写
<style name="dialog_soft_input" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowSoftInputMode">stateVisible|adjustPan</item>
</style>
第二步:写一个需要的EditText layout文件 edittext.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/frame_gray"
android:gravity="top"
android:maxLength="500"
android:padding="8dp"
android:textSize="13sp" />
第三步:用style构造AlertDialog,然后把自己的EditText加进去,加自己的操作然后show就好了
final EditText editText = (EditText) LayoutInflater.from(this).inflate(R.layout.edittext, null); AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.dialog_soft_input).setView(editText);
builder.setTitle("标题")
.setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { // do something
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
全部代码: http://download.csdn.net/download/suihs11/10041710
https://github.com/suihs11/SoftInputAlertDialog
|