如何在显示带有EditText的对话框时弹出软键盘?

4

我阅读了这里的一些帖子并尝试了谷歌搜索。但我仍然有这个问题:
我制作了一个子类自定义对话框。它包含一个EditText和一个按钮(“确定”)。我希望键盘在对话框弹出时自动显示。

我通过添加以下内容成功实现了此操作:

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);

在我的dismiss()方法中。

这会在对话框弹出时打开键盘,并在我按下“确定”按钮后关闭键盘。

然而,如果软键盘已经打开并且我按下手机/模拟器的HOME按钮,则键盘将保持打开状态,因为 - 我想 - 我使用SHOW_FORCED强制打开它。因此,我尝试在对话框的父活动的onPause()方法中隐藏(使用InputMethodManager的toggleSoftInput())键盘,如果它是打开的。这似乎只能通过一个解决方法来实现,如这里所示。

简而言之:我希望当我的带有EditText和Button的对话框弹出时显示软键盘(焦点在EditText上)。我已经做到了,但涉及编写许多关闭它的hack代码。

编辑:我基于这个代码。


可能是重复问题:https://dev59.com/vW855IYBdhLWcg3wlVYZ#19573049 - SparK
4个回答

2

这个问题在这里得到了解答,对我来说非常有效。如果我在键盘显示时按下主页按钮,它会在按下主页键后正确地隐藏。


0
@Override
public void onResume() {
    super.onResume();
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            try {
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInputFromWindow(view.getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
            } catch (Exception e) {

            }
        }
    }, 300);
}

类型为EditTextView的“view”。"context"是当前上下文。

希望能帮到您。


-1
editTextProjectName.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editTextProjectName,
InputMethodManager.SHOW_IMPLICIT);

你能否用言语解释一下你的解决方案? - Magnilex

-1
您可以使用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();
    }




}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接