编程语言
首页 > 编程语言> > 应用程序恢复后触发android – onQueryTextChange

应用程序恢复后触发android – onQueryTextChange

作者:互联网

我对动画的searchview onQueryTextListener感到沮丧.当首先创建活动和片段时,它工作得很好.然后我按下主页按钮,打开其他应用程序,在那里做一些工作来擦除searchview活动的数据,然后返回到应用程序.当活动和片段恢复onQueryTextChange方法由它自己触发时.我试过这个问题
Fragment replacement triggers onQueryTextChange on searchview
但它没有帮助,仅在searchview SHOW_AS_ACTION_NEVER时有帮助,但在这种情况下我看不到searchview.如何防止OnQueryTextListener的自触发?
来自片段的片段

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    searchView = new SearchView(getSherlockActivity().getSupportActionBar()
            .getThemedContext());
    searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (newText.length() > 0) {

                fpAdapter.getFilter().filter(newText);
            } else {

                loadData();

            }
            return false;
        }
    });

    TextView searchText = (TextView) searchView
            .findViewById(R.id.abs__search_src_text);
    searchText.setTextColor(Color.WHITE);

    searchText.setCursorVisible(false);

    ImageView searchButton = (ImageView) searchView
            .findViewById(R.id.abs__search_button);
    searchButton.setImageResource(R.drawable.search_menu_button);
    LinearLayout searchEditFrame = (LinearLayout) searchView
            .findViewById(R.id.abs__search_edit_frame);
    searchEditFrame.setBackgroundResource(android.R.color.transparent);

    View searchPlate = searchView.findViewById(R.id.abs__search_plate);
    searchPlate.setBackgroundColor(Color.argb(0, 0, 0, 0));
    menu.add("Search")

    .setActionView(searchView)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    final MenuItem searchMenuItem = menu.getItem(0);
    final Animation in = AnimationUtils.loadAnimation(getSherlockActivity()
            .getApplicationContext(), R.anim.search_in);
    final Animation out = AnimationUtils.loadAnimation(
            getSherlockActivity().getApplicationContext(),
            R.anim.search_out);
    searchView.setQueryHint(getResources().getText(
            R.string.search_messages_hint));
    searchView.setIconifiedByDefault(true);
    searchView
            .setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view,
                        boolean queryTextFocused) {

                    if (!queryTextFocused) {

                        // searchView.startAnimation(out);
                        searchMenuItem.collapseActionView();
                    } else {
                        searchView.startAnimation(in);

                    }
                }
            });

    super.onCreateOptionsMenu(menu, inflater);
}

更新:这只出现在Android 4.0.3的HTC sensation XL中,在4.2上我没有看到这个问题.

解决方法:

找到唯一的解决方案 – 在onResume中设置监听器:

@Override
public void onResume() {

    super.onResume();

    searchView.setOnQueryTextListener(new OnQueryTextListener() {

    @Override
    public boolean onQueryTextSubmit(String query) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        if (newText.length() > 0) {

            fpAdapter.getFilter().filter(newText);
        } else {

            loadData();

        }
        return false;
    }
}); }

标签:onresume,android,triggers,searchview,textchanged
来源: https://codeday.me/bug/20190830/1769097.html