ActionBar上的Android SearchView,检测点击搜索

时间:2014-06-20 18:39:20

标签: android android-searchmanager android-search

在我的应用程序中,与许多其他应用程序一样,有一个常见的搜索小部件(http://developer.android.com/guide/topics/search/search-dialog.html#UsingSearchWidget)。

只有按下键盘上的搜索按钮,我才会更改片段(并将搜索词传递给它)。

我已经尝试了

searchView.setOnSearchClickListener(new OnClickListener() { 
@Override public void onClick(View v) { 
} });

但是当你按下动作栏上的按钮时会触发它,它会解锁搜索输入,而不会在点击键盘按钮搜索时触发。

你知道怎么可能吗?

6 个答案:

答案 0 :(得分:15)

您可以使用此功能检测搜索键,按下操作栏搜索:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String s) {
                [You actions here]
                return false;
            }
}

答案 1 :(得分:1)

这是我用于编辑文本的代码。类似的事情对你有用:

editTextSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
      //run query to the server
    }
    return false;
}
});

答案 2 :(得分:1)

试用此代码。它非常小而且理解。

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);


        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();
        final EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);

        searchEditText.setHint("Search");

        searchEditText.setHintTextColor(getResources().getColor(R.color.white));
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

        searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //run query to the server
                    Log.e("onClick: ", "-- " + searchEditText.getText().toString().trim());

                }
                return false;
            }
        });


        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        return super.onOptionsItemSelected(item);
    }

menu.xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always" />

</menu>

答案 3 :(得分:0)

首先,将 SearchView 的IME选项设置为IME_ACTION_SEARCH(虽然键盘可能会自动设置)

  

searchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);

接下来,检查键盘上是否按了 搜索图标

+-----+-------+-----+
| id  | Name  |Votes|
+-----+-------+-----+
|  1  | Joe   | 36  |
|  2  | John  | 34  |
|  3  | Mark  | 42  |
|  4  | Ryan  | 29  |
|  5  | Jay   | 36  |
|  6  | Shawn | 39  |
+-----+-------+-----+

注意:此处,当按下键盘上的搜索图标时,会调用 onQueryTextSubmit()

此外, serchView clearFocus() 会折叠键盘。

希望这是你正在寻找的。

答案 4 :(得分:0)

对我来说,我从SearchView中获取了EditText,并捕获了“ setOnEditorActionListener”,但是默认的搜索动作仍在发生,提交后Activity刷新了。为了解决这个问题,我删除了

searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

通过我的onCreateOptionsMenu方法,现在一切运行正常

答案 5 :(得分:0)

 searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener()
    {
        @Override
        public void onSearchViewShown()
        {
            //write your code
        }

        @Override
        public void onSearchViewClosed()
        {
            //write your code 
        }
    });

希望这会有所帮助。