禁用SearchView [英] Disabling SearchView

查看:269
本文介绍了禁用SearchView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的Activity禁用SearchView.我尝试了以下代码:

I'm trying to disable a SearchView from my Activity. I tried the following code :

    mSearchView.setEnabled(false);
    mSearchView.setFocusable(false);
    mSearchView.setClickable(false);

但是它不起作用.仍然可以单击SearchView并弹出键盘.我希望它变灰且不可点击.我该怎么做?

But it does not work. SearchView can still be clicked and KeyBoard popps up. I want it to become greyed out and unclickable. How can I accomplish this?

推荐答案

没有一个陈述的答案足以满足我的需要,因此我想为处于相同情况的任何人提供另一个答案.

None of the stated answers were sufficient for my needs, so I would like to provide another one for anybody in the same situation.

SearchView由不同的View组成,这些View可以-并且在这种情况下必须-分别寻址. 如果您希望SearchView(支持v7)在这种状态下冻结并变灰,而不是对任何类型的输入(包括搜索和清除"按钮)进行回答,则可以使用:

A SearchView is made up of different Views, which can be - and in this case have to be - addressed individually. If you want your SearchView (support v7) to freeze and grey out in a state like this, not answering to any kind of input, including the search and clear button, you can use:

ImageView clearButton = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
SearchView.SearchAutoComplete searchEditText = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);

clearButton.setEnabled(false);
searchEditText.setEnabled(false);
searchView.setSubmitButtonEnabled(false);

(另外,我对Deeptis的回答searchView.setInputType(InputType.TYPE_NULL)遇到问题:如果您以此方式禁用SearchView并随后单击它,则系统似乎希望打开键盘,尽管该键盘未显示.因此,第一个后退按钮单击只会导致关闭-未显示或实际上未打开-键盘.)

( Besides, I got an issue with deeptis answer searchView.setInputType(InputType.TYPE_NULL): If you disable the SearchView this way and click it afterwards, the system seemingly expects an open keyboard though the keyboard isn't shown. Therefore, the first back button click causes nothing but closing the - not shown or not in fact opened - keyboard. )

这篇关于禁用SearchView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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