更改标签时如何隐藏软键盘?

Cod*_*ate 6 android android-listview

编辑:似乎我没有说清楚.我需要的是一种隐藏软键盘的方法,每当我更换我所在的片段时.我该怎么做呢?

让我保持这个简单.我在Tab Fragment 1.2中有一个EditText框,显然在按下时会打开软键盘.更改选项卡时如何隐藏?我在我的onTabSelected()中尝试了以下操作,它似乎没有做任何事情

getWindow().setSoftInputMode(
                  WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)

我现在已经尝试了一切.到目前为止,我所提出的解决方案都没有以任何方式帮助我.

小智 12

以编程方式,您可以使用,捕获设备屏幕上活动活动的视图.

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

private void hiddenKeyboard(View v) {
        InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        keyboard.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
Run Code Online (Sandbox Code Playgroud)


Jaz*_*Jaz 6

我遇到了同样的问题,并在使用FragmentTransaction.replace()方法更改标签之前将以下代码放在我的标签片段中.在初始选项卡选择之后,不会触发每个片段中的onCreateonCreateView方法,因此可以在到达特定片段的类之前隐藏键盘.使用mTabHost.getApplicationWindowToken()而不是editText.getWindowToken()一个主要的帮助.感谢无论谁找到了.对不起,我丢失了链接.

InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);             
im.hideSoftInputFromWindow(mTabHost.getApplicationWindowToken(), 0);

.......
fm = getFragmentManager();
....

fm.beginTransaction()
    .replace(placeholder, new someFragment(), tabId)
    .commit();
Run Code Online (Sandbox Code Playgroud)