安卓:在自定义AlertDialog显示软键盘上的EditText焦点 [英] Android: Show Soft Keyboard in Custom AlertDialog on EditText Focus

查看:509
本文介绍了安卓:在自定义AlertDialog显示软键盘上的EditText焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义AlertDialog上来,但是当你在布局中的EditText领域挖掘,软键盘不会自动出现。我想这个解决方案<一个href=\"http://stackoverflow.com/questions/9102074/android-edittext-in-dialog-doesnt-pull-up-soft-keyboard\">Android:在的EditText对话框不拉软键盘使用:

<$c$c>dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

因为我没有把code在正确的位置可能是那样简单。我试了一下在onCreateDialog和活动的prepareDialog以及自定义AlertDialog的构造和的onCreate。没有工作。

我倒是preFER这个解决方案,因为这似乎是最佳做法,在想有一个聚焦状态侦听器的EditText领域。

我如何已经尝试过在活动

  @覆盖
保护对话框onCreateDialog(INT ID){
    对话的对话;
    开关(ID){
    案例LOCATION_DETAILS_DIALOG:
        对话框=新LocationDetails(这一点,detailsS​​etListener);
        dialog.getWindow()setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)。
        返回对话框;    默认:
        返回null;
    }
}在prepareDialog保护无效(最终诠释ID,最后对话的对话){
    super.on prepareDialog(ID,对话);
    开关(ID){
    案例LOCATION_DETAILS_DIALOG:
       dialog.getWindow()setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)。
    }
}

我如何已经尝试过在AlertDialog类

 公共LocationDetails(上下文的背景下,DetailsS​​etEventListener detailsS​​etEventListener){
    超级(上下文);
    this.context =背景;
    this.detailsS​​etEventListener = detailsS​​etEventListener;
    dialog.getWindow()setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)。
}@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    dialog.getWindow()setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)。

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


解决方案

<$p$p><$c$c>dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

对于我来说工作得很好,我把它在构造函数中,如

 公共CustomDialog(上下文的背景下){
        超级(上下文);
        显示();
        的setContentView(R.layout.widget_custom_dialog);        getWindow()。clearFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    }

更改AlertDialog到对话框将导致我的错误对话框中的位置,所以我用这个方法。

I have a custom AlertDialog come up, but when you tap on the EditText fields in the layout, the soft keyboard doesn't automatically come up. I tried this solution Android: EditText in Dialog doesn't pull up soft keyboard using:

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

It might be as simple as I am not putting the code in the correct location. I tried it in the onCreateDialog and onPrepareDialog of the Activity as well as the constructor and onCreate of the custom AlertDialog. That did not work.

I'd prefer this solution as this would seem best practices over trying to have a onFocus listener for the EditText fields.

How I've tried it in the Activity

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

How I've tried it in the AlertDialog class

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

Any ideas why this does not work?

解决方案

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

works fine for me, I put it in the constructor, such as

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

Change AlertDialog to Dialog will cause the wrong dialog position for me, so I use this method.

这篇关于安卓:在自定义AlertDialog显示软键盘上的EditText焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆