我已经尝试了很多方法,但我无法在我的 Activity 中显示任何键盘。键盘就是不显示!!!我的 Activity 中有一个按钮,当单击该按钮时,它会调用下面的方法,该方法会在内部和顶部(标题中)创建一个带有 ListView 的对话框,其中有一个编辑文本。
我希望用户在 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();
}
在 list 中我有这个 Activity :
<activity android:name="com.tresipunt.iturist.Ventas2"
android:label="@string/ventas"
android:screenOrientation="portrait"
android:configChanges="keyboard">
</activity>
当对话框打开时,edittext 接缝获得焦点,它会改变颜色,当我调试 fovus 状态时,相应地更改为 true 或 false
我尝试过但不起作用的事情: * 删除取消按钮 * 将焦点监听器直接添加到 dittext 而不是 ListView ,但这没有帮助 * alertDialog 的代码: 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 个听众对他来说太多了)但这不起作用 但似乎没有用,我已经检查了其他有类似问题的链接,但要么没有解决方案,要么对我不起作用:
Programmatically Hide/Show Android Soft Keyboard
1 Android: show soft keyboard automatically when focus is on an EditText
欢迎任何建议,或者我做错了什么? 非常感谢,
最佳答案
我解决了!!!
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();
只需要使用一个简单的对话框而不是 AlerertDialog 加上构建器....
关于Android KeyBoard 不显示在 AlertDialog 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7281322/