問題描述:dialog中editText被軟鍵盤擋住。
1.在你的dialog的xml文件最外層添加:
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.core.widget.NestedScrollView>
2.你的dialog要繼承Android系統(tǒng)的Dialog
class AppUpdateDialog2(context: Context) :
Dialog(context, R.style.CustomDialog) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val layoutParams = window?.attributes
layoutParams?.apply {
width = WindowManager.LayoutParams.WRAP_CONTENT
height = WindowManager.LayoutParams.WRAP_CONTENT
gravity = Gravity.CENTER
}
window?.attributes = layoutParams
setContentView(R.layout.edit_email_dialog_layout)
//setCanceledOnTouchOutside(false)//點(diǎn)擊外部不消失
//setCancelable(false)//點(diǎn)擊返回鍵不消失
//這行很關(guān)鍵
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN or
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
}
}
<style name="CustomDialog" parent="android:style/Theme.Dialog">
<!--背景顏色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除標(biāo)題 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除邊框-->
<item name="android:windowFrame">@null</item>
<!--是否浮現(xiàn)在activity之上-->
<item name="android:windowIsFloating">true</item>
</style>
3.使用
val appUpdateDialog =
activity?.let { AppUpdateDialog2(it) }
appUpdateDialog?.show()