我有一個自定義的AlertDialog出現,但是當您點擊佈局中的EditText字段時,軟鍵盤不會自動出現。我想這個解決方案Android: EditText in Dialog doesn't pull up soft keyboard使用:Android:在EditText上的自定義AlertDialog上顯示軟鍵盤Focus
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
那還不如我不把代碼放在正確的位置一樣簡單。我在Activity的onCreateDialog和onPrepareDialog以及自定義AlertDialog的構造函數和onCreate中嘗試了它。這沒有用。
我更喜歡這種解決方案,因爲這似乎是嘗試爲EditText字段設置onFocus偵聽器的最佳實踐。
如何我已經嘗試過在活動
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch (id) {
case LOCATION_DETAILS_DIALOG:
dialog = new LocationDetails(this, detailsSetListener);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
return dialog;
default:
return null;
}
}
protected void onPrepareDialog(final int id,final Dialog dialog){
super.onPrepareDialog(id, dialog);
switch (id) {
case LOCATION_DETAILS_DIALOG:
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}
}
我如何已經嘗試過在AlertDialog類
public LocationDetails(Context context, DetailsSetEventListener detailsSetEventListener) {
super(context);
this.context = context;
this.detailsSetEventListener = detailsSetEventListener;
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
任何想法,爲什麼這不起作用?
所以我假設AlertDialog禁止鍵盤,因爲它只是提供一個消息和確定/取消按鈕。它應該只是用作提示。 所以我只是將我的自定義對話框的基類更改爲對話框。通過改變我的自定義Dialog繼承的基類,EditText字段將在他們獲得焦點時拉起軟鍵/虛擬鍵盤,就像您期望的那樣。 – mplspug 2012-02-19 00:40:39