应用程序恢复后触发onQueryTextChange

时间:2014-08-12 19:27:32

标签: android triggers searchview onresume textchanged

我对动画的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上我没有看到这个问题。

4 个答案:

答案 0 :(得分:3)

找到唯一的解决方案 - 在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;
    }
}); }

答案 1 :(得分:2)

使用此代码

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(final String s) {

            if(searchView.getWidth()>0)
            {
                // your code here
            }

            return false;
        }
    });

答案 2 :(得分:2)

您可以设置library(dplyr) library(purrr) df %>% transmute(new = map2(b, e, `:`)) %>% pull(new) %>% flatten_int %>% replace(logint, ., TRUE) 仅在焦点对准时使用。我有一个类似的问题,每次用户恢复搜索时,搜索都会在我的片段中执行。我用以下方法解决它:

-添加一个布尔值以查看何时SearchView处于焦点:

SearchView

//by default the SearchView isn't in focus so set it to false.
private boolean shouldSearch = false;

答案 3 :(得分:0)

这是一个已知问题。您可以通过检查搜索是否“图标化”来修复,即只显示放大镜图标或它是否已展开(这意味着用户正在与搜索视图交互)

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                //your code here
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                //this will catch firing event
                if (searchView.isIconified) {
                    return true
                }
                //your code here
                return true
            }
        })