隐藏键盘时,片段事务的自定义动画消失

时间:2013-11-22 17:57:30

标签: android android-fragments android-animation android-softkeyboard

我有一个带有4个碎片的平板电脑应用程序(MyNavigationBarMaster,MyNavigationBarDetails,Master e Details)。

我使用Fragment事务和自定义动画(iphon风格),如下所示:

FragmentTransaction mfragmentTransaction = getFragmentManager().beginTransaction();
mfragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_out_right, R.anim.slide_in_right);
mfragmentTransaction.replace(R.id.MenuMFragment, new PeopleMFragment());
mfragmentTransaction.addToBackStack(null).commit();

没问题,但当我尝试在PeopleMFragment()中使用此代码以编程方式隐藏键盘时:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
}

片段事务的动画不起作用,但键盘被隐藏,片段连接正确。

为什么呢?问题是键盘和xml动画有一个单独的线程? 有其他人有同样的问题?提前谢谢

1 个答案:

答案 0 :(得分:1)

尝试一下。 (我试过htc欲望)

public void nextFragment(){

    //close keyboard
    InputMethodManager inputMethodManager = (InputMethodManager)  getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);

    //remove focus, to remove any "auto-suggestions" views from the text field
    getActivity().getCurrentFocus().clearFocus();

    //change fragment
    Fragment2 newFragment = new Fragment2();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
    transaction.replace(R.id.fragmentContainer, newFragment);
    transaction.addToBackStack(null);
    transaction.commit();

}//nextFragment