Android:在EditText Focus上的Custom AlertDialog中显示软键盘

时间:2012-02-13 14:30:46

标签: android android-edittext alertdialog show android-softkeyboard

我有一个自定义AlertDialog,但是当您点击布局中的EditText字段时,软键盘不会自动出现。我使用以下方法尝试了此解决方案Android: EditText in Dialog doesn't pull up soft keyboard

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);

为什么这不起作用的任何想法?

1 个答案:

答案 0 :(得分:9)

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会导致我的对话位置错误,所以我使用此方法。

相关问题