Android 强制打开软键盘

Android 强制打开软键盘,android,Android,我试图在活动中强制打开软键盘,并抓取输入的所有内容,因为我想自己处理输入,我没有编辑文本。目前我已经试过了,但不起作用。我想在mAnswerTextView下面打开软键板(注意:这是一个TextView而不是EditText) 如何强制打开软键盘 我该如何对输入的每一个字符进行快速处理。我想在处理完软键盘后刷新每个字符。也就是说,用户不能在软键盘上输入整个单词 您可能需要有某种类型的可编辑文本区域来获得焦点。不过,您可能会有一个不可见的或透明的背景上没有光标。您可能需要玩弄视图的聚焦设置 使用T

我试图在活动中强制打开软键盘,并抓取输入的所有内容,因为我想自己处理输入,我没有编辑文本。目前我已经试过了,但不起作用。我想在mAnswerTextView下面打开软键板(注意:这是一个TextView而不是EditText)

  • 如何强制打开软键盘
  • 我该如何对输入的每一个字符进行快速处理。我想在处理完软键盘后刷新每个字符。也就是说,用户不能在软键盘上输入整个单词

  • 您可能需要有某种类型的可编辑文本区域来获得焦点。不过,您可能会有一个不可见的或透明的背景上没有光标。您可能需要玩弄视图的聚焦设置

    使用TextWatcher检查对EditText的编辑,或者如果需要更低级别的钩子,请使用其setOnKeyListener()方法设置textview的键侦听器。看

    使用此调用强制打开软键盘:

    ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
        .showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);
    
    这是要关闭它的一个:

    ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
        .hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
    

    请注意,这确实不是建议的-强制打开键盘有点乱。您的用例是什么,它确实需要您在不使用普通编辑框的情况下接受用户输入,并且需要在不回显的情况下逐个键地接受用户输入?

    尝试以下方法以强制打开软键盘:

    ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    
    ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(_pay_box_helper.getWindowToken(), 0);
    
    然后,您可以使用以下代码关闭键盘:

    ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    
    ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(_pay_box_helper.getWindowToken(), 0);
    

    为了强制打开键盘,我使用了

    this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    

    这对我来说很有效。

    有时候其他的答案不起作用。
    这是另一种方法

    它将通过收听窗口焦点强制键盘显示活动开始的时间
    onWindowFocusChanged()
    它将清除并请求编辑文本的焦点,然后将软输入模式设置为可见,并将选择设置为框中的文本。如果您是从活动中调用它,则此操作应始终有效

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            mEditText.clearFocus();
            mEditText.requestFocus();
            getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            mEditText.setSelection(mEditText.getText().toString().length());
        }
    }
    
    您可能还需要

    mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
                }
            }
        });
    

    编辑:我还看到嵌套片段中的键盘未打开,请注意此类情况。

    如果要控制软键盘内部活动,请使用以下代码:

    //create soft keyboard object
    InputMethodManager imm = (InputMethodManager)this.getSystemService(INPUT_METHOD_SERVICE);
    
    //1.USE
    your_view.setFocusableInTouchMode(true); //Enable touch soft keyboard to this view
    //or
    your_view.setFocusable(true); //Enable keyboard to this view
    imm.showInputMethod(your_view, InputMethodManager.SHOW_IMPLICIT);
    
    //2.USE show keyboard if is hidden or hide if it is shown
    imm.toggleSoftInputFromWindow(your_view.getWindowToken(),InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
    //or
    imm.toggleSoftInputFromWindow(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
    
    //3.USE (you cannot control imm)
    this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    
    //4.USE (with Dialog)
    Dialog d = new Dialog(this, android.R.style.Theme_Panel);
    d.getWindow().setTitle(null);
    d.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    d.setOnKeyListener(keyListener);
    d.setCanceledOnTouchOutside(true);
    d.setCancelable(true);
    d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    d.show();
    
    //to hide keyboard call:
    d.dismiss();
    //if you want get soft keyboard visibility call:
    d.isShowing();
    

    我已经测试过了,这是有效的:

    。。。 //显示软键盘

    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    
    //要隐藏它,请再次调用该方法

    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    

    可悲的是,尽管我很想投票支持其中一个答复,但没有一个对我有效。似乎解决方案是等待布局阶段完成。在下面的代码中,请注意我是如何检查
    showKeyboard
    方法是否返回TRUE的,此时我删除了全局布局侦听器。如果不这样做,它就会一败涂地。现在它似乎工作得很好

    理想情况下,您需要在
    onResume()中执行以下操作


    工作很好………

    edt_searchfilter_searchtext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    edt_searchfilter_searchtext.post(new Runnable() {
                        @Override
                        public void run() {
                            InputMethodManager imm = (InputMethodManager) getFragmentActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.showSoftInput(edt_searchfilter_searchtext, InputMethodManager.SHOW_IMPLICIT);
                        }
                    });
                }
            }
        });
    
    当您想打开键盘时,请拨打线下通话

     edt_searchfilter_searchtext.requestFocus();
    

    简单地说,使用添加两行将非常有效:

    如果使用XML

    android:focusable="true"
    android:focusableInTouchMode="true"
    
    Java中的Else:

    view.setFocusableInTouchMode(true);
    view.requestFocus();
    

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

    我从机器人那里得到了大量的错误。特别是java.lang.IndexOutOfBoundsException:getChars(6…0)具有先结束后开始的bug。这是由于EditText由于什么EditText?这个错误来自哪里,TextWatcher、KeyListener或showSoftInput/hideSoftInput调用?您还必须发布代码和堆栈跟踪,以便任何人都能在这里为您提供帮助。showSoftInput()在第一次尝试时并不总是起作用。我发现我必须在视图上重复我的操作2次才能显示。重要提示:
    InputMethodManager.show\u FORCED
    不是
    showsoftwoint()
    方法的有效标志!只有
    0
    InputMethodManager.SHOW\u IMPLICIT
    是有效标志!请注意执行此命令的时刻。有时,还没有呈现视图。谢谢。“关闭键盘”示例中的“付款箱”助手是什么?编辑:啊,我明白了,这是一个文本字段变量。对我来说非常有用,在可折叠菜单上编辑动作栏上的文本Sherlock。谢谢。在棒棒糖中我的片段的编辑文本唯一有效的解决方案。嗨,我和你有同样的问题。我可以显示键盘,但如何在没有编辑文本的情况下抓取输入的所有内容?塔克斯!对我来说是可行的,但您必须在“onCreate”中的“super.onCreate()”不起作用之前调用它。并且,在super in.onCreate之前调用它会创建一个空值。方法也错了。请解释一下为什么/如何回答这个问题?不鼓励只使用代码的答案,因为它们不像带有解释的代码那么容易学习。在没有解释的情况下,理解所做的工作、对代码所做的更改、代码是否回答了问题等需要花费相当多的时间和精力。解释对于试图从答案中学习的人和评估答案的人来说都很重要,以确定答案是否有效,或者值得投票。糟糕的拷贝和粘贴嵌套的片段是我努力的方向。可能是真的
        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();
        }
    
    
    
    
    }