搜索查看侦听IME行动 [英] SearchView listen for IME actions

查看:88
本文介绍了搜索查看侦听IME行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能侦听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;
    }

});

这篇关于搜索查看侦听IME行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆