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

之后编写的编辑场和击 Button, 我想要隐藏在虚拟键盘。我认为这是一个简单的代码,但是我在哪里可以找到一个例子?

有帮助吗?

解决方案

要帮助澄清这种疯狂,我想道歉代表所有Android用户对谷歌的软键盘的荒谬透顶治疗开始。之所以有这么多的答案,每一个不同的,同样简单的问题,因为这个API,许多人一样在Android中,是可怕的设计。我能想到的没有礼貌的方式来说明它。

我要隐藏键盘。我希望提供的Android使用以下语句:Keyboard.hide()。结束。非常感谢你。但是Android有一个问题。您必须使用InputMethodManager隐藏键盘。 OK,很好,这是Android的API键盘。但!您需要有一个Context,以获得访问IMM。现在我们有一个问题。我可能希望隐藏从没有对任何Context使用或需要一个静态或实用类键盘。或与远更糟的是,IMM需要指定什么View(或更糟的是,什么Window)要隐藏键盘FROM。

这是什么使隐藏的键盘很有挑战性。亲爱的谷歌:当我在找了一个蛋糕配方,有地球上没有任何RecipeProvider将拒绝向我提供的食谱,除非我先回答WHO蛋糕将由吃掉,它就会被吃掉!

此悲伤的故事与丑陋真相结束:隐藏Android键盘,你将被要求提供2种形式的标识:一个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或可聚焦的控制。它自然地遵循该INPUTMETHOD(通常软键盘)将通过示出自身到焦点事件做出响应。在windowSoftInputModeAndroidManifest.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();

其他提示

您可以强制的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传递的第二个参数,以确保您只隐藏键盘,当用户没有明确迫使其出现(按住菜单)。

注意:如果您想这样做在科特林,使用: context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

<强>科特林语法

// 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) }
 }

还包括用于隐藏软键盘有用的是:

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

这可以用来抑制软键盘,直到用户实际接触EDITTEXT视图。

我得到一个更溶液隐藏键盘:

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

下面通过HIDE_IMPLICIT_ONLYshowFlag0hiddenFlag的位置的位置。它会强行合软键盘。

·梅尔的解决方案适用于我。在我的情况,我的应用程序的顶层是一个tabHost,我想切换标签时隐藏关键字 - 我从tabHost视图窗口标记

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

请在onCreate()尝试这个下面的代码

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

<强>更新 我不知道为什么这个解决方案是不工作了(我只是测试Android上的23)。请使用href="https://stackoverflow.com/a/9494042/523325"> 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);
protected void hideSoftKeyboard(EditText input) {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(input.getWindowToken(), 0);    
}

如果一切都在这里其他的答案不为你工作,你会他们想,有手动控制键盘的另一种方式。

创建功能与将管理某些EditText的属性:

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

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

然后,请确保EditText的聚焦状态您打开/关闭键盘:

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

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 ... */
}

这使搜索,在这里我发现对我的作品的答案

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

简单的回答

在您的OnClick听者调用onEditorActionEditTextIME_ACTION_DONE

button.setOnClickListener(new OnClickListener() {

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

中的向下钻取

我觉得这种方法更好,更简单,更对准Android的设计模式。 在以上(和在大多数常见的情况通常情况下)的简单的例子,你就必须有一个EditText /曾重点,它通常也被调用摆在首位的键盘之一(它肯定是能够调用它许多常见的情况)。在这同样的方式,的应该是一个以释放键盘,通常可以通过一个ImeAction完成。刚看到EditTextandroid:imeOptions="actionDone"的行为方式,要实现以同样的方式相同的行为。


检查有关此答案

此应该工作:

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

我使用输入的十六进制数,所以我不能有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);
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);
    }
}

我花了超过两天的工作通过的所有解决方案公布在线程,并发现他们缺少一种或另一种方式。我确切的要求是要有一个按钮,将有100%可靠性显或隐藏的屏幕上的键盘。当键盘是在其隐藏的状态是不应该重新出现,无论是什么样的输入领域的用户击。当它在其可见的国家的键盘应该不会消失的无论什么按钮用户的点击。这一工作需要在安卓2.2+所有最新设备。

你可以看到一个工作执行这在我的应用程序 清洁RPN.

经过测试的许多建议的答复在一些不同的电话(其中包括取和姜饼设备)很明显,安卓应用程序可以可靠地:

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

这导致目前的活动具有其国家保存到捆绑,然后在活动开始,通过一个布尔这表明,如果应该键盘上显示或隐藏。

内部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设置。

这种方法可以可靠地工作的所有设备上我已经测试过在从一个4岁的宏达的手机运行的安卓2.2达nexus7运行4.2.2.唯一的缺点与这种做法是你必须要小心处理。作为我的应用程序,基本上只有一个屏幕(其一个计算器)我可以复盖onBackPressed()和返回的设备的主屏幕上。

另外,以这一切它的解决方案,如果你想关闭软键盘的从任何地方而不必是用来打开键盘,但仍然希望这样做,如果该领域的重点,你可以使用这个(从一个Activity)的(EditText上)字段的引用:

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

由于该SO回答时,我衍生这在我的情况下,通过滚动时很好地工作如下所述的一个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);
    }
}

以上答案为不同的情景的,但如果您想要隐藏视图中的键盘和努力得到正确的情况下尝试这方面的工作:

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

如果你想有一个单元或功能测试期间关闭软键盘,你可以从你的测试点击“返回键”这样做的:

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

我把“返回按钮”在引号,由于上述不触发用于所讨论的活动的onBackPressed()。它只是关闭键盘。

确认在移动之前暂停一会儿,因为它需要一些时间来关闭后退按钮,所以后续点击次数向视图等,将不能登记,直到短的暂停后(即1秒是足够长的时间IME)。

下面是你如何做到这一点在单为Android(AKA MonoDroid的)

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

这为我工作的所有怪异键盘行为

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

添加到清单文件,您的活动android:windowSoftInputMode="stateHidden"。例如:

<activity
            android:name=".ui.activity.MainActivity"
            android:label="@string/mainactivity"
            android:windowSoftInputMode="stateHidden"/>
public static void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

后onTouchListener该呼叫:

findViewById(android.R.id.content).setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Utils.hideSoftKeyboard(activity);
        return false;
    }
});

使用此

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

有关我的情况下,我使用的动作条的一个搜索查看。用户执行搜索之后,键盘会再次弹开。

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

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

就在你的活动使用此优化代码:

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

我的情况下,在我的EditText也可以位于一个AlertDialog,所以键盘应该在驳回关闭。下面的代码似乎在任何地方工作:

public static void hideKeyboard( Activity activity ) {
    InputMethodManager imm = (InputMethodManager)activity.getSystemService( Context.INPUT_METHOD_SERVICE );
    View f = activity.getCurrentFocus();
    if( null != f && null != f.getWindowToken() && EditText.class.isAssignableFrom( f.getClass() ) )
        imm.hideSoftInputFromWindow( f.getWindowToken(), 0 );
    else 
        activity.getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );
}

我几乎尝试了所有这些问题的答案,我有一些随机问题,尤其是与三星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);
}

在某些情况下,这种方法可以除了其他所有的作品。 这样可以节省我的日子。)

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);
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top