Android 键盘不显示在 AlertDialog 中
我尝试了很多方法,但我无法在我的活动中显示任何键盘。键盘就是不显示!我的活动中有一个按钮,当单击该按钮时,会调用下面的方法,该方法会创建一个内部有列表视图的对话框,顶部(标题中)有一个编辑文本。
我希望用户在 dit 文本中输入一个字母,数组适配器将被过滤并仅显示相关结果。因此,一切正常,除了没有显示键盘,因此您无法在 dit 文本中输入任何文本。这是代码:
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();
}
在清单中,我对此活动有这个:
<activity android:name="com.tresipunt.iturist.Ventas2"
android:label="@string/ventas"
android:screenOrientation="portrait"
android:configChanges="keyboard">
</activity>
当对话框打开时,edittext接缝会获得焦点,它会改变颜色,当我调试时,焦点状态会相应地更改为 true 或 false
我尝试过的事情那不起作用: * 删除取消按钮 * 将焦点侦听器直接添加到 dittext 而不是列表视图,但这没有帮助 * 警报对话框的代码: AlertDialog.requestFeature().addContentView(edi, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- 尝试使用 onclicklisteners 代替,但效果相同...
- 添加这些: //edi.setFocusable(true); //edi.setFocusableInTouchMode(true); //edi.requestFocus();
*删除 edi.addTextChangedListener(filterTextWatcher); (谁知道也许 2 个听众对他来说太多了)但这不起作用 但似乎没有任何作用,我已经检查了具有类似问题的其他链接,但要么没有解决方案,要么对我不起作用:
1 Android:焦点位于时自动显示软键盘EditText
欢迎任何建议,或者我做错了什么? 多谢,
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.
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. So everything works fine EXCEPT that there is no keyboard shown so you can't enter any text in the dit text. 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
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));
- trying with onclicklisteners instead but same effect...
- adding those:
//edi.setFocusable(true);
//edi.setFocusableInTouchMode(true);
//edi.requestFocus();
*removing the 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:
Programmatically Hide/Show Android Soft Keyboard
1
Android: show soft keyboard automatically when focus is on an EditText
Any advice would be welcome or is there something I am doing wrong?
Thanks a lot,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了!!!
只需使用一个简单的对话框而不是AlerertDialog加上构建器....
I solved it!!!
just have to use a simple dialog not AlerertDialog plus builder....