关闭/隐藏Android软键盘

问题描述 投票:3497回答:92

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

在编辑字段中写入并单击Button后,我想隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的一个例子?

android android-layout android-softkeyboard android-input-method soft-keyboard
92个回答
1603
投票

为了帮助澄清这种疯狂,我想首先代表所有Android用户道歉,谷歌对软键盘的彻头彻尾的荒谬处理。对于同样简单的问题,有这么多答案的原因有很多,因为这个API与Android中的许多其他API一样,设计非常糟糕。我认为没有礼貌的方式来陈述它。

我想隐藏键盘。我期望为Android提供以下声明:Keyboard.hide()。结束。非常感谢你。但Android存在问题。您必须使用InputMethodManager来隐藏键盘。好的,很好,这是Android的键盘API。但!您需要拥有Context才能访问IMM。现在我们遇到了问题。我可能想要将键盘隐藏在没有任何用途或需要任何Context的静态或实用程序类中。或者更糟糕的是,IMM要求您指定要隐藏键盘FROM的View(或更糟糕的是,Window)。

这使得隐藏键盘变得如此具有挑战性。亲爱的谷歌:当我查找蛋糕的配方时,地球上没有RecipeProvider拒绝向我提供配方,除非我第一次回答世界卫生组织,蛋糕将被吃掉,哪里会被吃掉!

这个悲伤的故事以丑陋的真相结束:要隐藏Android键盘,您需要提供两种形式的识别:Context以及ViewWindow

我已经创建了一个静态实用程序方法,只要你从Activity调用它就可以非常稳定地完成工作。

public static void hideKeyboard(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    //Find the currently focused view, so we can grab the correct window token from it.
    View view = activity.getCurrentFocus();
    //If no view currently has focus, create a new one, just so we can grab a window token from it
    if (view == null) {
        view = new View(activity);
    }
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

请注意,此实用程序方法仅在从Activity调用时才有效!上面的方法调用目标getCurrentFocusActivity来获取正确的窗口令牌。

但是假设你想要隐藏在EditText中的DialogFragment键盘?你不能使用上面的方法:

hideKeyboard(getActivity()); //won't work

这将无效,因为你将传递对Fragment的主机Activity的引用,当Fragment显示时,它将没有集中控制!哇!所以,为了从键盘中隐藏键盘,我采用较低级别,更常见,更丑陋:

public static void hideKeyboardFrom(Context context, View view) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

以下是从追求此解决方案浪费的更多时间中收集的一些其他信息:

关于windowSoftInputMode

还有另一个争论点需要注意。默认情况下,Android会自动将初始焦点分配给EditText中的第一个Activity或focusable控件。由此可见,InputMethod(通常是软键盘)将通过显示自身来响应焦点事件。 windowSoftInputMode中的AndroidManifest.xml属性设置为stateAlwaysHidden时,指示键盘忽略此自动分配的初始焦点。

<activity
    android:name=".MyActivity"
    android:windowSoftInputMode="stateAlwaysHidden"/>

几乎令人难以置信的是,当你触摸控件时,它似乎没有阻止键盘打开(除非focusable="false"和/或focusableInTouchMode="false"被分配给控件)。显然,windowSoftInputMode设置仅适用于自动焦点事件,而不适用于触摸由触摸事件触发的事件。

因此,stateAlwaysHidden的名字确实非常糟糕。它应该被称为ignoreInitialFocus而不是。

希望这可以帮助。


更新:获取窗口令牌的更多方法

如果没有焦点视图(例如,如果您刚刚更改了片段,则会发生),还有其他视图将提供有用的窗口令牌。

这些是上述代码if (view == null) view = new View(activity);的替代品。这些代码并未明确提及您的活动。

在片段类中:

view = getView().getRootView().getWindowToken();

给定一个片段fragment作为参数:

view = fragment.getView().getRootView().getWindowToken();

从您的内容正文开始:

view = findViewById(android.R.id.content).getRootView().getWindowToken();

更新2:如果您从后台打开应用程序,请清除焦点以避免再次显示键盘

将此行添加到方法的末尾:

view.clearFocus();


57
投票

到目前为止,Saurabh Pareek有最好的答案。

不过,不妨使用正确的标志。

/* hide keyboard */
((InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE))
    .toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

/* show keyboard */
((InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE))
    .toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);

实际使用的例子

/* click button */
public void onClick(View view) {      
  /* hide keyboard */
  ((InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE))
      .toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

  /* start loader to check parameters ... */
}

/* loader finished */
public void onLoadFinished(Loader<Object> loader, Object data) {
    /* parameters not valid ... */

    /* show keyboard */
    ((InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE))
        .toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);

    /* parameters valid ... */
}

52
投票

从搜索到这里,我找到了一个适合我的答案

// Show soft-keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

// Hide soft-keyboard:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

49
投票

简短的回答

在你的OnClick听众用onEditorAction调用EditTextIME_ACTION_DONE

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        someEditText.onEditorAction(EditorInfo.IME_ACTION_DONE)
    }
});

深入研究

我觉得这种方法更好,更简单,更符合Android的设计模式。在上面的简单示例中(通常在大多数常见情况下),您将拥有一个具有焦点的EditText,它通常也是首先调用键盘的键盘(它肯定能够调用它)许多常见情况)。以同样的方式,它应该是释放键盘的那个,通常可以由ImeAction完成。只要看看EditTextandroid:imeOptions="actionDone"的表现如何,你想用同样的方法达到同样的行为。


检查这个related answer


44
投票

这应该工作:

public class KeyBoard {

    public static void show(Activity activity){
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); // show
    }

    public static void hide(Activity activity){
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide
    }

    public static void toggle(Activity activity){
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (imm.isActive()){
            hide(activity); 
        } else {
            show(activity); 
        }
    }
}

KeyBoard.toggle(activity);

40
投票

我正在使用自定义键盘输入十六进制数字,因此我无法显示IMM键盘...

在v3.2.4_r1中添加了setSoftInputShownOnFocus(boolean show)来控制天气或者在TextView获得焦点时不显示键盘,但是它仍然隐藏所以必须使用反射:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    try {
        Method method = TextView.class.getMethod("setSoftInputShownOnFocus", boolean.class);
        method.invoke(mEditText, false);
    } catch (Exception e) {
        // Fallback to the second method
    }
}

对于旧版本,我使用OnGlobalLayoutListener获得了非常好的结果(但远非完美),在我的根视图中添加了ViewTreeObserver,然后检查键盘是否显示如下:

@Override
public void onGlobalLayout() {
    Configuration config = getResources().getConfiguration();

    // Dont allow the default keyboard to show up
    if (config.keyboardHidden != Configuration.KEYBOARDHIDDEN_YES) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mRootView.getWindowToken(), 0);
    }
}

最后一个解决方案可能会显示键盘一瞬间,并与选择手柄混淆。

当键盘进入全屏时,不会调用onGlobalLayout。要避免这种情况,请使用TextView#setImeOptions(int)或TextView XML声明:

android:imeOptions="actionNone|actionUnspecified|flagNoFullscreen|flagNoExtractUi"

更新:刚刚发现哪些对话框用于永不显示键盘并适用于所有版本:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
        WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

31
投票
public void setKeyboardVisibility(boolean show) {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if(show){
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }else{
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);
    }
}

28
投票

我花了两天多的时间来完成线程中发布的所有解决方案,并发现它们缺乏这种或那种方式。我的确切要求是有一个100%可靠性的按钮显示或隐藏屏幕键盘。当键盘处于隐藏状态时,无论用户点击什么输入字段,都不应重新出现。当它处于可见状态时,无论用户点击什么按钮,键盘都不会消失。这需要在Android 2.2+上运行,直到最新的设备。

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

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

  1. 暂时隐藏键盘。当用户聚焦新文本字段时,它将再次重新出现。
  2. 在活动开始时显示键盘,并在活动上设置一个标志,指示键盘应始终可见。只有在活动初始​​化时才能设置此标志。
  3. 将活动标记为从不显示或允许使用键盘。只有在活动初始​​化时才能设置此标志。

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

不幸的是,列表中的第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()并返回到设备主屏幕。


25
投票

作为this all around solution的替代方案,如果你想从任何地方关闭软键盘而没有引用用于打开键盘的(EditText)字段,但是如果字段是聚焦的话仍然想要这样做,你可以使用它(来自一个活动):

if (getCurrentFocus() != null) {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}

23
投票

感谢this SO answer,我推导出以下内容,在我的情况下,滚动浏览ViewPager的片段时效果很好...

private void hideKeyboard() {   
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

private void showKeyboard() {   
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }
}

20
投票

上面的答案适用于不同的场景但是如果你想在视图中隐藏键盘并努力获得正确的上下文,请尝试以下方法:

setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        hideSoftKeyBoardOnTabClicked(v);
    }
}

private void hideSoftKeyBoardOnTabClicked(View v) {
    if (v != null && context != null) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

并从构造函数获取上下文:)

public View/RelativeLayout/so and so (Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
    init();
}

4320
投票

您可以强制Android使用InputMethodManager隐藏虚拟键盘,调用hideSoftInputFromWindow,传入包含焦点视图的窗口的标记。

// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

这将强制键盘在所有情况下都被隐藏。在某些情况下,您需要传入InputMethodManager.HIDE_IMPLICIT_ONLY作为第二个参数,以确保在用户未明确强制显示键盘时(仅通过按住菜单)隐藏键盘。

注意:如果您想在Kotlin中执行此操作,请使用:context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

Kotlin语法

// Check if no view has focus:
 val view = this.currentFocus
 view?.let { v ->
  val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager 
  imm?.let { it.hideSoftInputFromWindow(v.windowToken, 0) }
 }

18
投票

如果要在单元或功能测试期间关闭软键盘,可以通过单击测试中的“后退按钮”来执行此操作:

// Close the soft keyboard from a Test
getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);

我把“后退按钮”放在引号中,因为上面没有触发有问题的活动的onBackPressed()。它只是关闭键盘。

确保在继续之前暂停一段时间,因为关闭后退按钮需要一些时间,因此后续点击视图等,直到短暂停顿后才会注册(1秒足够长时间) )。


15
投票

以下是你在Mono for Android(AKA MonoDroid)中的表现

InputMethodManager imm = GetSystemService (Context.InputMethodService) as InputMethodManager;
if (imm != null)
    imm.HideSoftInputFromWindow (searchbox.WindowToken , 0);

15
投票

这对我来说非常适合所有奇怪的键盘行为

private boolean isKeyboardVisible() {
    Rect r = new Rect();
    //r will be populated with the coordinates of your view that area still visible.
    mRootView.getWindowVisibleDisplayFrame(r);

    int heightDiff = mRootView.getRootView().getHeight() - (r.bottom - r.top);
    return heightDiff > 100; // if more than 100 pixels, its probably a keyboard...
}

protected void showKeyboard() {
    if (isKeyboardVisible())
        return;
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if (getCurrentFocus() == null) {
        inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    } else {
        View view = getCurrentFocus();
        inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }
}

protected void hideKeyboard() {
    if (!isKeyboardVisible())
        return;
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    View view = getCurrentFocus();
    if (view == null) {
        if (inputMethodManager.isAcceptingText())
            inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
    } else {
        if (view instanceof EditText)
            ((EditText) view).setText(((EditText) view).getText().toString()); // reset edit text bug on some keyboards bug
        inputMethodManager.hideSoftInputFromInputMethod(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

14
投票

添加到您的活动android:windowSoftInputMode="stateHidden" in Manifest文件。例:

<activity
            android:name=".ui.activity.MainActivity"
            android:label="@string/mainactivity"
            android:windowSoftInputMode="stateHidden"/>

12
投票

用这个

this.getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

12
投票

对于我的情况,我在操作栏中使用了SearchView。用户执行搜索后,键盘将再次弹出。

使用InputMethodManager没有关闭键盘。我不得不clearFocus并将搜索视图的焦点设置为false:

mSearchView.clearFocus();
mSearchView.setFocusable(false);

12
投票

只需在您的活动中使用此优化代码:

if (this.getCurrentFocus() != null) {
    InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

12
投票

我几乎尝试了所有这些答案,我有一些随机问题,尤其是三星Galaxy s5。

我最终得到的是强迫节目和隐藏,它完美地运作:

/**
 * Force show softKeyboard.
 */
public static void forceShow(@NonNull Context context) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}

/**
 * Force hide softKeyboard.
 */
public static void forceHide(@NonNull Activity activity, @NonNull EditText editText) {
    if (activity.getCurrentFocus() == null || !(activity.getCurrentFocus() instanceof EditText)) {
        editText.requestFocus();
    }
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

12
投票

在某些情况下,除了所有其他方法外,此方法可以起作这节省了我的一天:)

public static void hideSoftKeyboard(Activity activity) {
    if (activity != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (activity.getCurrentFocus() != null && inputManager != null) {
            inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
            inputManager.hideSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
        }
    }
}

public static void hideSoftKeyboard(View view) {
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputManager != null) {
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}

12
投票

简单易用的方法,只需调用hideKeyboardFrom(YourActivity.this);隐藏键盘

/**
 * This method is used to hide keyboard
 * @param activity
 */
public static void hideKeyboardFrom(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

775
投票

隐藏软键盘也很有用:

getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);

这可用于在用户实际触摸editText视图之前抑制软键盘。


11
投票

对于打开键盘:

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

326
投票

我还有一个隐藏键盘的解决方案:

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

这里通过HIDE_IMPLICIT_ONLYshowFlag0的位置在hiddenFlag的位置。它会强行关闭软键盘。


142
投票

Meier的解决方案也适用于我。在我的情况下,我的应用程序的顶级是tabHost,我想在切换标签时隐藏关键字 - 我从tabHost视图中获取窗口标记。

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    public void onTabChanged(String tabId) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(tabHost.getApplicationWindowToken(), 0);
    }
}

130
投票

请在onCreate()中尝试以下代码

EditText edtView=(EditText)findViewById(R.id.editTextConvertValue);
edtView.setInputType(0);

122
投票

更新:我不知道为什么这个解决方案不再起作用(我刚刚在Android 23上测试过)。请使用Saurabh Pareek的解决方案。这里是:

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
//Hide:
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
//Show
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

老答案:

//Show soft-keyboard:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//hide keyboard :
 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

79
投票
protected void hideSoftKeyboard(EditText input) {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(input.getWindowToken(), 0);    
}

66
投票

如果这里的所有其他答案对您不起作用,那么还有另一种手动控制键盘的方法。

创建一个函数来管理一些EditText的属性:

public void setEditTextFocus(boolean isFocused) {
    searchEditText.setCursorVisible(isFocused);
    searchEditText.setFocusable(isFocused);
    searchEditText.setFocusableInTouchMode(isFocused);

    if (isFocused) {
        searchEditText.requestFocus();
    }
}

然后,确保你打开/关闭键盘的EditText的onFocus:

searchEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (v == searchEditText) {
            if (hasFocus) {
                // Open keyboard
                ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(searchEditText, InputMethodManager.SHOW_FORCED);
            } else {
                // Close keyboard
                ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
            }
        }
    }
});

现在,每当你想手动打开键盘时调用:

setEditTextFocus(true);

并且为了结束通话:

setEditTextFocus(false);
© www.soinside.com 2019 - 2024. All rights reserved.