简体   繁体   English

Android创建自定义SearchView搜索操作

[英]Android Create custom SearchView Search action

In my application I am using the Action Bar with a custom SearchView in it. 在我的应用程序中,我使用的是带有自定义SearchView的操作栏。 I want to use some custom code when the user hits the enter button in the search field. 当用户在搜索字段中按下Enter键时,我想使用一些自定义代码。 But I cant find a way to catch the moment the user hits the enter button in the SearchView. 但是我找不到一种方法来捕获用户在SearchView中按下Enter键的那一刻。

Now I tried several ways to capture the search action but I cant seem to find it. 现在,我尝试了几种方法来捕获搜索动作,但似乎无法找到它。

My button is defined like this: 我的按钮定义如下:

<item
    android:id="@+id/action_bar_search"
    android:title="Search"
    android:icon="@drawable/ic_menu_search"
    android:showAsAction="always"
    android:actionViewClass="-package-.CustomSearchView"
    />

With this belongs the CustomSearchView class: CustomSearchView类与此相关:

public class CustomSearchView extends SearchView implements OnClickListener, android.view.View.OnKeyListener{

    public CustomSearchView(Context context) {
        super(context);
        this.setOnSearchClickListener(this);
        this.setSubmitButtonEnabled(true);
            this.setOnClickListener(this);
        this.setOnKeyListener(this);
    }

    @Override
    public void onClick(View v) {

        Log.d("SEARCH", "Onclick! " + this.getQuery());
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        Log.d("SEARCH", "Search onkey");
        return false;
    }

}

Here you can see that I`ve tried 2 approaches: 在这里您可以看到我尝试了两种方法:

  1. Catch the onclick event from the submit button. 从提交按钮捕获onclick事件。 That didnt work since it only fired when I clicked on the search icon to open the SearchView. 那没有用,因为它仅在我单击搜索图标以打开SearchView时才触发。
  2. Catch keystrokes within the SearchView. 在SearchView中捕获击键。 That didnt work either. 那也不起作用。 Got no response from any key that I pressed there. 我按那里的任何键都没有反应。

Does anyone have a way of making a custom Search action with this SearchView? 有没有人可以使用此SearchView进行自定义搜索操作?

UPDATE: 更新:

As stated below by PJL: You dont need a custom dialog for overriding this behaviour. 如下面的PJL所述:您不需要自定义对话框来覆盖此行为。 The normal SearchView widget will work just fine for this. 普通的SearchView窗口小部件对此可以正常工作。

Get your search view and add an OnQueryTextListener 获取搜索视图并添加一个OnQueryTextListener

final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextChange(String newText) {
        // Do something
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        // Do something
        return true;
    }
};

searchView.setOnQueryTextListener(queryTextListener);

By the way my menu layout is as follows; 顺便说一下,我的菜单布局如下:

<item android:id="@+id/menu_search"
    android:showAsAction="ifRoom"
    android:actionViewClass="android.widget.SearchView" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM