搜索查看侦听IME行动行动、IME

2023-09-03 21:26:20 作者:伤痛能耐我何

我如何能侦听IME行动搜索查看?由于它不是一个子类的EditText

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

我想要做的是处理当用户presses回车键在键盘上的并没有进入任何文本。

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

与那里是没有文字的问题是, OnQueryTextListener.onQueryTextSubmit()只有当用户输入文本触发。

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

推荐答案

如何获取搜索查看的EditText上,并使用OnEditorAction?

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;
    }

});