我正在使用输入框AlertDialog.在EditText该对话框中本身自动对焦,当我打电话AlertDialog.show(),但软键盘未自动显示.
如何在显示对话框时自动显示软键盘?(并且没有物理/硬件键盘).与按下"搜索"按钮调用全局搜索的方式类似,将自动显示软键盘.
我有一个EditText和一个Button.点击按钮我想打开EditText键盘并同时请求重点EditText,以便用户直接开始键入键盘和文本出现在EditText.但在我的情况下,当我单击按钮时,它会打开键盘,但它不会将焦点设置在EditText,因为用户必须EditText再次单击它才能在其上书写.问题是什么 任何帮助或建议.
代码单击按钮
m_SearchEditText.requestFocus();
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(m_SearchEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
Run Code Online (Sandbox Code Playgroud) 我已经阅读了这篇文章,它会在显示一个对话框时自动显示虚拟键盘.但是,它不适合我.任何想法为什么?虽然当对话框出现时编辑文本自动聚焦,但事件不会触发.我也读过onpostresume的答案,但我不知道如何应用它.任何帮助表示赞赏.
final Dialog dialog = new Dialog(ThesisI.this);
dialog.setContentView(R.layout.budget_dialog);
final EditText et = (EditText) dialog.findViewById(R.id.textComments);
final Button enter = (Button) dialog.findViewById(R.id.buttonEnter);
final Button cancel = (Button) dialog.findViewById(R.id.buttonCancel);
enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
/**cancel */
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
dialog.show();
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
Run Code Online (Sandbox Code Playgroud)
但是,我注意到如果我将焦点更改为按钮,则再次聚焦到编辑文本.此事件有效,使用下面的代码.
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean …Run Code Online (Sandbox Code Playgroud) 我试图创建一个包含EditTexts的RecyclerView的DialogFragment。当我单击EditText时,它会滚动并显示“复制/剪切/粘贴”,但键盘永远不会出现。自从我尝试在Activity中实现RecyclerView以来,适配器就起作用了。
我已经尝试过寻找使键盘显示出来的解决方案,例如将其添加到XML中。
</request focus>
Run Code Online (Sandbox Code Playgroud)
还是这个到对话框
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Run Code Online (Sandbox Code Playgroud)
但仍然没有任何效果。
非常感谢。
其他:这是目前的样子
android android-softkeyboard android-edittext android-recyclerview
我有一个自定义AlertDialog,但是当你点击布局中的EditText字段时,软键盘不会自动出现.我尝试了这个Android解决方案:Dialog中的EditText不会使用以下方式拉起软键盘:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我是如何在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); …Run Code Online (Sandbox Code Playgroud) android show android-softkeyboard android-edittext android-alertdialog