[英]Android: Show Soft Keyboard in Custom AlertDialog on EditText Focus
我有一個自定義的AlertDialog,但是當您單擊布局中的EditText字段時,軟鍵盤不會自動出現。 我在Android上嘗試了此解決方案:Dialog中的EditText不會使用以下命令拉出軟鍵盤 :
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);
任何想法為什么這不起作用?
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
對我來說效果很好,我把它放在構造函數中,例如
public CustomDialog(Context context) {
super(context);
show();
setContentView(R.layout.widget_custom_dialog);
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}
將AlertDialog更改為Dialog會對我造成錯誤的對話框位置,因此我使用此方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.