Android 当我显示带有编辑文本的对话框时,如何使软键盘显示?
我在这里读了几篇帖子,还尝试过谷歌搜索。但我仍然有这个问题:Android 当我显示带有编辑文本的对话框时,如何使软键盘显示?,android,dialog,android-edittext,android-softkeyboard,Android,Dialog,Android Edittext,Android Softkeyboard,我在这里读了几篇帖子,还尝试过谷歌搜索。但我仍然有这个问题: 我制作了一个子类自定义对话框。它包含一个编辑文本和一个按钮(“确定”)。我想在对话框弹出后自动显示键盘 我成功地做到了这一点: imm = (InputMethodManager) EditDialog.this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
我制作了一个子类自定义对话框。它包含一个编辑文本和一个按钮(“确定”)。我想在对话框弹出后自动显示键盘 我成功地做到了这一点:
imm = (InputMethodManager) EditDialog.this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
在自定义对话框的onCreate()中
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
在我的字典里()
当对话框弹出时,这会打开键盘,当我按下“确定”按钮时,也会关闭键盘
但是,如果软键盘打开,我按下手机/模拟器的HOME(主页)按钮,它们的键盘将保持打开状态,因为-我想-我用SHOW_FORCED强制打开它。因此,如果键盘在对话框的父活动onPause()方法中打开,我尝试隐藏键盘(使用InputMethodManager中的toggleSoftInput())。如图所示,这似乎只可能使用变通方法
TL;DR:我希望在弹出带有编辑文本和按钮的对话框时显示软键盘(关注编辑文本)。我得到了工作,但它涉及到编写许多黑客关闭它正确
编辑:我的代码基于这得到了回答,它对我来说非常有用。如果在显示键盘时按home(主页)按钮,则按home(主页)键后,键盘将正确隐藏
editTextProjectName.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editTextProjectName,
InputMethodManager.SHOW_IMPLICIT);
和EditTextView类型的“视图”。“上下文”是当前上下文
Wish可以帮助您。您可以使用这个KeyboardHelper.java类来显示和隐藏键盘
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
* Created by khanhamza on 06-Mar-17.
*/
public class KeyboardHelper {
public static void hideSoftKeyboard(Context context, View view) {
if (context == null || view == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public static void hideSoftKeyboardForced(Context context, View view) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromInputMethod(view.getWindowToken(), 0);
}
public static void hideSoftKeyboard(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
public static void showSoftKeyboard(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
editText.requestFocus();
}
public static void showSoftKeyboardForcefully(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
editText.requestFocus();
}
}
您能否用文字解释您的解决方案?可能重复:
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
* Created by khanhamza on 06-Mar-17.
*/
public class KeyboardHelper {
public static void hideSoftKeyboard(Context context, View view) {
if (context == null || view == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public static void hideSoftKeyboardForced(Context context, View view) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromInputMethod(view.getWindowToken(), 0);
}
public static void hideSoftKeyboard(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
public static void showSoftKeyboard(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
editText.requestFocus();
}
public static void showSoftKeyboardForcefully(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
editText.requestFocus();
}
}