我的布局中有一个EditText和一个Button。

在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?


当前回答

首先,您应该从XML文件中添加addandroid:imeOptions字段,并将其值更改为actionUnspecified|actionGo,如下所示

 <android.support.design.widget.TextInputEditText
                    android:id="@+id/edit_text_id"
                    android:layout_width="fill_parent"
                    android:layout_height="@dimen/edit_text_height"
                    android:imeOptions="actionUnspecified|actionGo"
                    />

然后在java类中添加setOnEditorActionListener并添加InputMethodManager,如下所示

enterOrderNumber.setOnEditorActionListener(新的TextView.OnEditorActionlister(){

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_GO) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
            return true;
        }
        return false;
    }
});

其他回答

简单编码:在onCreate()中使用此代码

getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);
public static void closeInput(final View caller) {  
    caller.postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) caller.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(caller.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }, 100);
}

此方法通常有效,但有一个条件:不能设置android:windowSoftInputMode=“any_of_themes”

我已经尝试了所有的解决方案,但没有一个解决方案适合我,所以我找到了我的解决方案:您应该有如下布尔变量:

public static isKeyboardShowing = false;
InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);

在触摸屏事件中,您可以拨打:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(keyboardShowing==true){
        inputMethodManager.toggleSoftInput(InputMethodManager.RESULT_UNCHANGED_HIDDEN, 0);
        keyboardShowing = false;
    }
    return super.onTouchEvent(event);
}

在EditText中的任何位置:

yourEditText.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            yourClass.keyboardShowing = true;

        }
    });

对于开放式键盘:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edtView, InputMethodManager.SHOW_IMPLICIT);

对于关闭/隐藏键盘:

 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.hideSoftInputFromWindow(edtView.getWindowToken(), 0);

我花了两天多的时间研究了帖子中发布的所有解决方案,发现它们在某种程度上有所欠缺。我的确切要求是有一个按钮,可以100%可靠地显示或隐藏屏幕键盘。当键盘处于隐藏状态时,无论用户单击什么输入字段,都不应再次出现。当键盘处于可见状态时,不管用户单击什么按钮,都不应该消失。这需要在Android 2.2+上运行,直到最新的设备。

你可以在我的应用程序clean RPN中看到这一点的有效实现。

在许多不同的手机(包括froyo和姜饼设备)上测试了许多建议的答案后,很明显,android应用程序可以可靠地:

暂时隐藏键盘。当用户聚焦新的文本字段。活动开始时显示键盘并在活动上设置一个标志,指示他们的键盘应该始终可见。仅当活动为初始化。将活动标记为从不显示或允许使用键盘仅当活动为初始化。

对我来说,暂时隐藏键盘是不够的。在某些设备上,一旦新的文本字段被聚焦,它就会重新出现。当我的应用程序在一个页面上使用多个文本字段时,聚焦一个新的文本字段将导致隐藏的键盘再次弹出。

不幸的是,列表中的第2项和第3项仅在活动开始时工作可靠。一旦活动可见,就不能永久隐藏或显示键盘。诀窍是当用户按下键盘切换按钮时实际重新启动活动。在我的应用程序中,当用户按下切换键盘按钮时,会运行以下代码:

private void toggleKeyboard(){

    if(keypadPager.getVisibility() == View.VISIBLE){
        Intent i = new Intent(this, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        Bundle state = new Bundle();
        onSaveInstanceState(state);
        state.putBoolean(SHOW_KEYBOARD, true);
        i.putExtras(state);

        startActivity(i);
    }
    else{
        Intent i = new Intent(this, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        Bundle state = new Bundle();
        onSaveInstanceState(state);
        state.putBoolean(SHOW_KEYBOARD, false);
        i.putExtras(state);

        startActivity(i);
    }
}

这会导致当前活动将其状态保存到一个Bundle中,然后启动该活动,并传递一个布尔值,该布尔值指示键盘是显示还是隐藏。

在onCreate方法中,运行以下代码:

if(bundle.getBoolean(SHOW_KEYBOARD)){
    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(newEquationText,0);
    getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
else{
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
            WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}

如果应该显示软键盘,则会告知InputMethodManager显示键盘,并指示窗口使软输入始终可见。如果软键盘应隐藏,则设置WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM。

这种方法在我测试过的所有设备上都能可靠地工作,从运行android 2.2的4岁HTC手机到运行4.2.2的nexus 7。这种方法的唯一缺点是需要小心处理后退按钮。由于我的应用程序基本上只有一个屏幕(它是一个计算器),我可以覆盖onBackPressed()并返回设备主屏幕。