片段替换时显示/隐藏Android软键盘

时间:2014-09-04 22:23:06

标签: android android-fragments fragment android-softkeyboard

我有一个带片段的活动。让我们说一个包含事物列表的列表片段。现在我想让用户添加一个东西,所以我使用FragmentManager用一个带有EditText的插入片段替换列表片段。 EditText具有焦点,光标闪烁。但软键盘无法打开。 反之亦然:如果用户输入了新内容并将其添加到列表中,我将插入片段替换为列表片段。但是,虽然没有EditText,键盘也不会关闭。

实现这个的正确方法是什么?我无法相信我必须在所有过渡时手动显示和隐藏键盘?!

9 个答案:

答案 0 :(得分:5)

我愿意遵循以下事项:

 1.扩展<html lang=""> <head> <!--Polymer--> <script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script> <link rel="import" href="bower_components/iron-icon/iron-icon.html"> <link rel="import" href="bower_components/iron-icons/iron-icons.html"> <link rel="import" href="bower_components/paper-toolbar/paper-toolbar.html"> <link rel="import" href="bower_components/paper-header-panel/paper-header-panel.html"> <!-- Page styles --> <style> body{ height: 100%; width:100%; margin:0px; padding: 0px; } .contentHolder { height: 2000px; } paper-header-panel { height:100%; @apply(--shadow-elevation-2dp); } paper-toolbar { height: 60px; font-size: 25px; line-height: 60px; font-weight: 300; padding: 0 10px; color: #FFF; transition: height 0.2s; } paper-toolbar.medium-tall{ line-height: 60px; font-size: 35px !important; } </style> </head> <body fullbleed layout vertical> <paper-header-panel flex mode="waterfall-tall" tall-class="medium-tall"> <paper-toolbar class="toolbar"> <div class="flex">Title</div> <iron-icon icon="search"></iron-icon> <iron-icon icon="menu"></iron-icon> <div class="middle"></div> </paper-toolbar> <div class="contentHolder"></div> </paper-header-panel> </body> </html> 班级  2.覆盖FragmentonAttach()回调  3.实现显示和隐藏软件键盘方法

示例代码:

onDetach()

答案 1 :(得分:4)

显示键盘:

editText.requestFocus();
InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(editText, 0);

然后隐藏它:

InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);

答案 2 :(得分:3)

我的app有同样的问题,最后我们有2个选项,首先创建一个通用函数并在所有转换中调用它或者创建一个全局转换如何:

public static void RemoveAndReplaceFragment(FragmentManager fragmentManager, int FragmentContent, Fragment PlaceHolderFragment,Activity context){
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(fragmentManager.findFragmentById(R.id.frgMainActivity))
                .commit();

        //Close keyBoard in transition
        InputMethodManager inputManager = (InputMethodManager) context.getSystemService(
                Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);

        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.animation_fade_in,R.anim.animation_fade_out);
        fragmentTransaction.replace(R.id.frgMainActivity, new PlaceholderFragment_MainActivity_AcceptAndFollowTask()).commit();
    }
}

最好的方法是捕捉过渡事件,但我们现在不能......告诉我,如果我帮助你,祝你好运!

答案 3 :(得分:1)

这肯定会有效:

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(){
    EditText myEditText = (EditText) findViewById(R.id.myEditText);  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
   }

答案 4 :(得分:0)

我猜你的ListView正在窃取你EditText的焦点,试试这个并告诉我它是否有帮助。

getListView().setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

答案 5 :(得分:0)

试试这个

InputMethodManager imgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

    imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    editText.requestFocus();

答案 6 :(得分:0)

我同意,我不相信你也必须手动隐藏键盘。

将此添加到您不希望键盘不在的片段onCreate中:

   ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(mToolbar.getWindowToken(), 0);

将mToolbar更改为布局中的任何视图。在这种情况下,我使用我的工具栏。

将此添加到您希望显示键盘的Fragment onCreate。

        ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

答案 7 :(得分:0)

您可以使用以下代码隐藏软键盘。

InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);

答案 8 :(得分:-1)

一种方法是调用Activity.recreate,因为无论如何都需要处理方向更改案例。看起来像矫枉过正,你最终也会得到不同的过渡动画。

相关问题