Android:如何检测点击自定义 SearchView 元素

标签 android android-layout android-searchmanager

我正在创建一个自定义 SearchView,但到目前为止,如果用户单击 searchView 小部件,我还没有找到(单击)事件或监听器。所以 SearchView 关闭,用户点击搜索图标(哪个监听器?!),SearchView 被打开。见下图。我需要那个事件,因为我想在用户打开 searchView 后更改 UI

enter image description here

编辑: 到目前为止我尝试的是

  • setOnClickListener 到 SearchView 元素 -> 但只有在 SearchView 打开时才会触发(第二张图片)

  • 搜索了 SearchView Api,但没有该操作的监听器(或者我是瞎了吗?)

  • 尝试了使用 searchView 的 onWindowFocusChanged 的解决方法,但由于 searchView 隐藏在某种 youtube/facebook 侧边栏中,这不起作用

有什么想法吗?我也附上了我的 SearchView 代码,但不要认为代码是错误的。只是还没有听众。顺便说一句:欢迎任何解决方法。但是,如果 SearchView 一开始就关闭,那就太好了! (如上图)

public class AmplifySearchView extends SearchView implements
        OnQueryTextListener, OnFocusChangeListener,
        OnCloseListener {

    private static final String DEBUG_TAG = "AmplifySeachView";

    private Context context;
    private DataBaseController datasource;
    private StationListView searchResultListView;
    private TextView noResultTextView;

    private Boolean searchStarted = false;

    public AmplifySearchView(Context context) {
        super(context);
        this.context = context;
    }

    public AmplifySearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public void initSearchView(StationListView view, TextView noResultTextView) {
        this.searchResultListView = view;
        this.searchResultListView.setActivityId(Constants.UI_SEARCH_RESULT_LIST);
        this.noResultTextView = noResultTextView;


        this.setOnQueryTextListener(this);
        this.setOnFocusChangeListener(this);
        this.setOnCloseListener(this);
        this.setOnCloseListener(this);

        setIconifiedByDefault(true);

        datasource = DataBaseController.getInstance(context);

        int id = this.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) findViewById(id);
        textView.setTextColor(Color.WHITE);


        resetSearchInputTextField();
    }

    private void resetSearchInputTextField() {
        setQueryHint(getResources().getString(R.string.search_hint));
    }

    @Override
    public boolean onClose() {
        Log.d(DEBUG_TAG, "onClose()");
        searchResultListView.setVisibility(View.GONE);
        noResultTextView.setVisibility(View.GONE);

        searchStarted = false;

        Intent intent = new Intent(Constants.SEARCH_ENDED);
        intent.addCategory(Constants.UI_AMPLIFY_CATEGORY);
        context.sendBroadcast(intent);

        return false;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        Log.d(DEBUG_TAG,"onFocusChange()" + hasFocus);

    }


    @Override
    public boolean onQueryTextChange(String newText) {

        //do sth...
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        Log.d(DEBUG_TAG, "onQueryTextSubmit -> " + query);
        return true;
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction,
            Rect previouslyFocusedRect) {
        Log.d(DEBUG_TAG, "onQueryTextSubmit -> " + gainFocus);
        // TODO Auto-generated method stub
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    }

    @Override
    public void onActionViewCollapsed() {
        Log.d(DEBUG_TAG, "onActionViewCollapsed");
        super.onActionViewCollapsed();
    }

    @Override
    public void onActionViewExpanded() {
        Log.d(DEBUG_TAG, "onActionViewExpanded");
        super.onActionViewExpanded();
    }
}

顺便说一句:焦点更改在这里不起作用,因为搜索 View 在开始时是不可见的,稍后插入到用户界面中......

最佳答案

这个怎么样?

public void setOnSearchClickListener (View.OnClickListener listener)

来自 android 开发者网站的描述说.. 设置监听器以在按下搜索按钮时进行通知。这仅在默认情况下不可见文本字段时才相关。调用 setIconified(false) 也可以导致此监听器被通知。

http://developer.android.com/reference/android/widget/SearchView.html#setOnSearchClickListener(android.view.View.OnClickListener)

关于Android:如何检测点击自定义 SearchView 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14602807/

相关文章:

android - 安卓的json utf-8编码

android - 未调用 Firebase onDataChange

android - 拥有一个 Activity 实例

android - SearchView 在一项 Activity 中,结果在另一项 Activity 中

Android 搜索 onCancel() 监听器未被调用

android - 客户端连接到服务器时出现超时错误

Android ellipsize 不能正常工作

java - 单击按钮时数字增加

android Activity 交易风格?

Android 可搜索配置未附加到 SearchView