强制打开软键盘

时间:2010-03-19 18:00:19

标签: android

我试图在一个Activity中强制打开Soft Keyboard并抓取输入的所有内容,因为我想自己处理输入,我没有EditText。目前我已经尝试了这个,但它不起作用。我想在mAnswerTextView下面打开Soft Keyboardto(注意:它是TextView而不是EditText)。

    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    // only will trigger it if no physical keyboard is open
    mgr.showSoftInput(mAnswerTextView, InputMethodManager.SHOW_IMPLICIT);
  1. 如何强制打开软键盘
  2. 如何获取输入的所有内容,以便我可以处理每个角色。我想在处理之后从软键盘中清除每个字符。即,用户不应该在软键盘中输入整个单词。

12 个答案:

答案 0 :(得分:126)

尝试此操作强制打开软键盘:

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

答案 1 :(得分:15)

您可能需要具有某种可编辑的文本区域才能获得焦点。但是,您可能有一个不可见或透明背景,没有光标。您可能需要使用视图的可聚焦设置。

使用TextWatcher检查是否使用addTextChangedListener对EditText进行了编辑,或者如果需要更低级别的钩子,请使用setOnKeyListener()方法设置textview的键侦听器。请参阅KeyListener documentation

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

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
    .showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);

这个关闭它:

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

请注意,这是不推荐的 - 强制打开键盘有点乱。什么是你的用例,真正需要你在没有普通编辑框的情况下接受用户输入,并且需要逐个按键地输入用户输入而不回复它?

答案 2 :(得分:8)

要强制键盘打开我使用

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

它对我有用。

答案 3 :(得分:5)

有时其他答案不会起作用 这是另一种方式..

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

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

编辑:我也看到键盘在嵌套片段中未打开,请注意这些情况。

答案 4 :(得分:1)

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

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

答案 5 :(得分:1)

可悲的是,尽管我喜欢对其中一个回复进行投票,但没有一个对我有用。似乎解决方案是等待布局阶段完成。在下面的代码中,请注意我如何检查showKeyboard方法是否返回TRUE,并且当我删除全局布局侦听器时。没有做到这一点,它被击中和错过。现在看起来效果很好。

您需要在onResume()

中理想地执行以下操作
@Override
public void onResume()
{
    super.onResume();

    ViewTreeObserver vto = txtTaskTitle.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
            {
                @Override
                public void onGlobalLayout()
                {
                    if (txtTaskTitle.requestFocus())
                    {
                        if (showKeyboard(getContext(), txtTaskTitle))
                        {
                            txtTaskTitle.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        }
                    }
                }
            });
}

public static boolean showKeyboard(Context context, EditText target)
{
        if (context == null || target == null)
        {
            return false;
        }

        InputMethodManager imm = getInputMethodManager(context);

        ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);

        boolean didShowKeyboard = imm.showSoftInput(target, InputMethodManager.SHOW_FORCED);
        if (!didShowKeyboard)
        {
            didShowKeyboard = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);
        }
        return didShowKeyboard;
}

答案 6 :(得分:0)

if(search.getText().toString().trim().isEmpty()) {
    InputMethodManager imm = (InputMethodManager)getSystemService(
              Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
}

答案 7 :(得分:0)

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

... //显示软键盘

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

//隐藏它,再次调用该方法

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

答案 8 :(得分:0)

工作得很好.........

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

答案 9 :(得分:0)

简单地说,使用添加2行就像魅力一样:

如果使用XML

android:focusable="true"
android:focusableInTouchMode="true"

其他Java:

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

答案 10 :(得分:0)

  

对于kotlin用户

全部功劳归于Dmitry's answer

在片段中打开键盘

根据我的要求,我已将mainActivity变量传递给fragment constructor,也许根据{{1},不推荐此hack /补丁}(我不知道)。

但是我们开始...

AndroidGuidLines

答案 11 :(得分:-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();
    }




}
相关问题