键盘未隐藏在ActionBar Tab更改上

时间:2012-07-26 05:43:40

标签: android android-actionbar android-tabs

我已在我的应用中实施了ActionBar标签。但是我在标签更改期间面临一个问题。我的标签主要包含webview,但一个标签包含编辑文本。 当我点击编辑文本键盘时出现,如果我正在更改标签键盘出现,则键盘不会消失。我尝试了一些简单的解决方案,比如明确隐藏它,但没有成功。

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 mgr.hideSoftInputFromWindow(fragment.getView().getApplicationWindowToken(), 0);

这是我在实现ActionBar.TabListener的类的onTabSelected()中调用的。我不知道如何解决这个问题,也没有得到相关信息。

提前致谢。任何帮助将不胜感激。

更新和回答

埃里克的回答有点让我感动,并帮助我找到了答案,所以我的答案与我的改变一致。即我在我的onTabUnselected中添加了eric的代码,但没有在tabSelected中添加,因为当我试图获取视图时,视图未被创建,因此将视图视为null。所以我的最终代码是

@Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft)
    {
        View target = initialisedFragment.getView().findFocus();

        if (target != null) 
        {
            InputMethodManager mgr = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            mgr.hideSoftInputFromWindow(target.getWindowToken(), 0);
        }
    }

2 个答案:

答案 0 :(得分:2)

我不相信您可以选择View并将其用作窗口令牌。您必须找到当前显示键盘的字段。

这是我之前使用的方法的一个端口,值得一试:

View target = fragment.getView().findFocus();
if (target != null) {
    InputMethodManager imm = (InputMethodManager) target.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(target.getWindowToken(), 0);
}

如果这不起作用,那就是lots of other methods that have been reported to work

答案 1 :(得分:0)

我使用以下选项来捕获设备正在运行的当前视图:

public final void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
        View focus = getCurrentFocus();
        if (focus != null) {
            hiddenKeyboard(focus);
        }
    }
public final void onTabselected(Tab tab, FragmentTransaction fragmentTransaction) {
        View focus = getCurrentFocus();
        if (focus != null) {
            hiddenKeyboard(focus);
        }
    }
public final void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
        View focus = getCurrentFocus();
        if (focus != null) {
            hiddenKeyboard(focus);
        }
    }