简体   繁体   English

Android KeyBoard未在AlertDialog中显示

[英]Android KeyBoard doesn't show in AlertDialog

I have tried so many ways but I just can't get the display any Keyboard in my activity. 我已经尝试了很多方法,但我无法在我的活动中显示任何键盘。 Keyboard just doesn't show!!! 键盘只是不显示! I have button in my activity that when clicked calls the method below which creates a dialog with a listview inside and on top (in the header) there is an edittext. 我在我的活动中有一个按钮,当点击时调用下面的方法创建一个对话框,里面有一个列表视图,在顶部(在标题中)有一个edittext。

I would like the user to enter a letter in the dit text and the array adapter will be filtered and will show only the relevant results. 我希望用户在dit文本中输入一个字母,数组适配器将被过滤,并仅显示相关结果。 So everything works fine EXCEPT that there is no keyboard shown so you can't enter any text in the dit text. 所以一切正常,除了没有显示键盘,所以你不能在dit文本中输入任何文字。 Here is the code: 这是代码:

private void buildDialog(final int cual) {

    // dialog que va mostrar una lista de clientes o articulos

    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    if(cual==1){
    mAdapter2 = new EventAdapter(this, clientes);
    }else if (cual==2){
        mAdapter2=new EventAdapter(this, ventas);
    }


    lv2=new ListView(this);
    edi=new EditText(this);
            edi.setHint("empiece a escribir");
    edi.addTextChangedListener(filterTextWatcher);
    lv2.addHeaderView(edi);
    lv2.setAdapter(mAdapter2);
    lv2.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(v.equals(edi) && hasFocus==true){
                      alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        }
    });
ab.setView(lv2);
alertDialog = ab.create();
alertDialog.setButton(getResources().getString(R.string.cancelar),
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                }
            });


    alertDialog.show();
}

In the manifest I have this for this activity: 在清单中我有这个活动:

<activity android:name="com.tresipunt.iturist.Ventas2"
        android:label="@string/ventas"
        android:screenOrientation="portrait"
        android:configChanges="keyboard">
    </activity>

The edittext seams to get the focus when the dialog opens it gets the color changed and when I debug the fovus state changes to true or to false accrodingly edittext接缝在对话框打开时获得焦点,它会改变颜色,当我调试fovus状态变为true或false时

The things I have tried and that don't work: * removing the cancel button * adding the focus listener to the dittext directly instead of the list view but it doesn't help * this code for alertDialog: alertDialog.requestFeature().addContentView(edi, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 我尝试过但不起作用的事情:*删除取消按钮*直接将焦点监听器添加到dittext而不是列表视图但它没有帮助* alertDialog的这段代码:alertDialog.requestFeature()。addContentView (edi,new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

  • trying with onclicklisteners instead but same effect... 尝试使用onclicklisteners但效果相同......
  • adding those: //edi.setFocusable(true); 添加://edi.setFocusable(true); //edi.setFocusableInTouchMode(true); //edi.setFocusableInTouchMode(true); //edi.requestFocus(); //edi.requestFocus();

*removing the edi.addTextChangedListener(filterTextWatcher); *删除edi.addTextChangedListener(filterTextWatcher); (Who knows maybe 2 listeners were too much for him) but it doesn't work but NOTHing seems to work and I have checked the other links with similar problem but either there isn't even a solution or it doesn't work for me: (谁知道可能有2个听众对他来说太多了)但是它不起作用但是看起来没有用,我检查了其他类似问题的链接,但要么没有解决方案,要么对我不起作用:

Programmatically Hide/Show Android Soft Keyboard 以编程方式隐藏/显示Android软键盘

[ http://groups.google.com/group/android-developers/browse_frm/thread/17210d784766602d/d430c900a9c4019c?pli=1] [ http://groups.google.com/group/android-developers/browse_frm/thread/17210d784766602d/d430c900a9c4019c?pli=1]

1 Android: show soft keyboard automatically when focus is on an EditText 1 Android:焦点在EditText上时自动显示软键盘

Any advice would be welcome or is there something I am doing wrong? 任何建议都会受欢迎,或者我做错了什么? Thanks a lot, 非常感谢,

I solved it!!! 我解决了!!!

Dialog dialog = new Dialog(this);
    if(cual==1){
        mAdapter2 = new EventAdapter(this, clientes);
    } else if (cual==2){
        mAdapter2=new EventAdapter(this, ventas);
    }


    lv2=new ListView(this);
    edi=new EditText(this);
    lv2.addHeaderView(edi);
    lv2.setAdapter(mAdapter2);
    dialog.setContentView(lv2);

    dialog.setCancelable(true);

    dialog.show();

just have to use a simple dialog not AlerertDialog plus builder.... 只需使用简单的对话框而不是AlerertDialog加构建器....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM