Android: How to detect click on custom SearchView element

烂漫一生 提交于 2019-11-28 21:07:31

How about this?

public void setOnSearchClickListener (View.OnClickListener listener)

Description from android developer website says.. Sets a listener to inform when the search button is pressed. This is only relevant when the text field is not visible by default. Calling setIconified(false) can also cause this listener to be informed.

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

 public void setOnSearchClickListener (View.OnClickListener listener)

solves the issue. actually i thought i ve been using this listener before, but the OnSearchClickListener isn't the OnClickListener in my SearchView. You have to declare that listener from the outside like

 AmplifySearchView sv = (AmplifySearchView) findViewById(...);

 sv.setOnSearchClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d("CLICK","CLICK");

        }
 });

for text field visible use:

SearchView searchView = (SearchView) findViewById(R.id.searchView);
    searchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //your code here
        }
    });

for text field invisible use:

searchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            //your code here
            return false;
        }
    });

Try this in Kotlin:

    searchView.setOnQueryTextFocusChangeListener { _ , hasFocus ->
        if (hasFocus) {
            // searchView expanded
        } else {
            // searchView not expanded
        }
    }

In Java :

searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            // searchView expanded
        } else {
            // searchView not expanded
        }
    }
});

This was written with API 15. It has not been tested. Details at bottom.

public class MySearchView extends SearchView {
    private boolean mHasFocus;
    private OnOpenListener mOnOpenListener;

    public MySearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnOpenListener(OnOpenListener onOpenListener) {
        mOnOpenListener = onOpenListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean onTouchEvent = super.onTouchEvent(event);

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (!mHasFocus) {
                mHasFocus = true;

                if (mOnOpenListener != null) {
                    mOnOpenListener.onOpen();
                }
            }

            break;
        }

        return onTouchEvent;
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

        if (!gainFocus) {
            mHasFocus = false;
        }
    }

    public interface OnOpenListener {
        public void onOpen();
    }
}

You give it an OnOpenListener and when the View is clicked it will fire the event. When it loses focus it will reset the boolean value. I think it will also fire the OnClose aswell that is already built in. What can be a problem is the focus bit; I have had problems before using a SearchView and it will not give up it's focus. Maybe you can outside of the class assign when the View shall have focus or not. If you want more features you have to build your own.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!