用於解決AlertDialog中需要向EditText輸入內容卻不能彈出輸入法
阿新 • • 發佈:2019-02-08
很多時候需要以AlertDialog對話方塊的形式和使用者進行互動,因為對於一些小資料資料資訊互動,而且可以不必要的開啟新的Activity而累贅。
但當AlerDialog裡面有EditText的時候需要輸入內容,而且是用這種方式設定佈局的話dialog.getWindow().setContentView(layout)就會出現彈不起軟鍵盤的尷尬。原來,通過閱讀官方文件: 其中有一段:
Note: Activities provide a facility to manage the creation, saving and restoring of dialogs. See , , ,
and.
If these methods are used, will
return the Activity that managed this dialog.
Often you will want to have a Dialog display on top of the current input method, because there is no reason for it to accept text. You can do this by setting the window flag (assuming your Dialog takes input focus, as it the default) with the following code:
getWindow().setFlags(WindowManager.LayoutParams這是預設情況下隱藏軟鍵盤的方法,要重新顯示軟鍵盤,要執行下面這段程式碼即可:.FLAG_ALT_FOCUSABLE_IM,WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
AlertDialog.setView()則不會出現以上問題。 另外:為了防止彈出輸入法時把後面的背景擠變形,可以在Manifest裡新增:android:windowSoftInputMode