去除碎片后如何清除焦点?

时间:2019-04-07 05:28:27

标签: android fragment android-fragmentactivity android-lifecycle

从应用程序中删除片段后,

问题明确显示了编辑文本。 再见,我在android 9中没有任何问题,但是我的真实设备安装了android 7,问题出现在那里

   searchFab.setOnClickListener(v -> {
            Fragment fragment = new SearchFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.setCustomAnimations(R.anim.fragment_bottom_up, R.anim.fragment_top_down
                    , R.anim.fragment_bottom_up, R.anim.fragment_top_down);
            transaction.replace(R.id.fragment_area, fragment);
            transaction.addToBackStack(null);
            if (searchEditText.isFocused()) {
                if (searchEditText.getText().toString().equals("")) {
                    inputMethodManager.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
                    searchEditText.clearFocus();
                    searchFabIcon.startAnimation(animationHide);
                    searchFabIcon.setImageResource(R.drawable.ic_search);
                    searchFabIcon.startAnimation(animationShow);
                    getSupportFragmentManager().popBackStack();
                } else {
                    searchEditText.setText("");
                }
            } else {
                transaction.commit();
                searchEditText.requestFocus();
                searchFabIcon.startAnimation(animationHide);
                searchFabIcon.setImageResource(R.drawable.ic_multiply);
                searchFabIcon.startAnimation(animationShow);
                inputMethodManager.showSoftInput(searchEditText,
InputMethodManager.SHOW_IMPLICIT);
            }
        });

1 个答案:

答案 0 :(得分:0)

最后,我找到了答案,只需要在布局xml文件中添加这两行。就我而言,我将它们放在父级布局中,就可以解决问题

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"