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())键盘。这似乎只能使用解决方法才能实现,如HERE所示。

TL;DR:我希望在弹出带有 EditText 和 Button 的对话框时显示软键盘(专注于 EditText)。我得到了这个工作,但它涉及编写许多黑客来正确关闭它。

编辑:我的代码基于这个

4

4 回答 4

2

这在这里得到了回答,它对我很有用。如果我在显示键盘时按下主页按钮,则在按下主页键后它会正确隐藏。

于 2011-04-26T22:19:40.260 回答
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 类型的“视图”。“上下文”是当前上下文。

Wish可以帮助你。

于 2016-04-28T10:53:32.273 回答
-1
editTextProjectName.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editTextProjectName,
InputMethodManager.SHOW_IMPLICIT);
于 2015-04-09T10:40:42.957 回答
-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();
    }




}
于 2019-08-19T13:50:00.317 回答