SearchView listen for IME actions

帅比萌擦擦* 提交于 2019-12-20 02:54:33

问题


How can I listen for IME actions on a SearchView? Since it is not a subclass of EditText.

What I want to do is to handle when the user presses the "enter" key on the keyboard and there is no text entered.

The problem with there being no text is that OnQueryTextListener.onQueryTextSubmit() is only triggered if the user has entered text.


回答1:


How about getting EditText of the searchview and using OnEditorAction?

final SearchView searchView = (SearchView) findViewById(R.id.searchView);
int searchViewPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchPlateEditText = (EditText) searchView.findViewById(searchViewPlateId);
searchPlateEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            if(!TextUtils.isEmpty(v.getText().toString())){
                //Text entered
            }
            else {
                //no string
            }
        }
        return true;
    }

});


来源:https://stackoverflow.com/questions/33384450/searchview-listen-for-ime-actions

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