禁用 SearchView 文本输入?

Make SearchView text entry disabled?

想知道有没有什么办法可以暂时让SearchView暂时不能用来打字但还能看到。我宁愿这样做也不愿为相同的 activity.

一遍又一遍地重新创建不同的操作栏

更新: 这里没有答案是堆栈跟踪:

05-08 18:51:38.981  18963-18963/com.parse.starter W/ResourceType﹕ No package identifier when getting value for resource number 0x00000001
05-08 18:51:38.989  18963-18963/com.parse.starter W/ImageView﹕ Unable to find resource: 1
android.content.res.Resources$NotFoundException: Resource ID #0x1
        at android.content.res.Resources.getValue(Resources.java:1019)
        at android.content.res.Resources.getDrawable(Resources.java:663)
        at android.widget.ImageView.resolveUri(ImageView.java:542)
        at android.widget.ImageView.onMeasure(ImageView.java:647)
        at android.view.View.measure(View.java:12752)
        at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
        at android.view.View.measure(View.java:12752)
        at android.widget.ListView.measureScrapChild(ListView.java:1181)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
        at android.widget.ListView.onMeasure(ListView.java:1155)
        at android.view.View.measure(View.java:12752)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
        at android.view.View.measure(View.java:12752)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
        at android.view.View.measure(View.java:12752)
        at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
        at android.view.View.measure(View.java:12752)
        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:848)
        at android.view.View.measure(View.java:12752)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
        at android.view.View.measure(View.java:12752)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
        at android.view.View.measure(View.java:12752)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2267)
        at android.view.View.measure(View.java:12752)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1074)
        at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2462)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4558)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)

来自http://developer.android.com/reference/android/widget/SearchView.html

对于值 0x00000000 - 没有内容类型。文本不可编辑。

所以试试这个:

mSearchView.setInputType(0x00000000);

并切换回可编辑状态:

mSearchView.setInputType(0x00000001);

您可以在 onCreateOptionsMenu 中轻松启用和禁用菜单项。 只需获取您的 searchView 并输入 setEnabled(false),如下所示:

MenuItem item= menu.findItem(R.id.action_search);
item.setEnabled(false);

希望对您有所帮助。