禁止SearchView文本输入?

时间:2015-05-08 20:42:08

标签: java android searchview

我想知道是否有办法临时制作,以便SearchView暂时无法用于输入但仍然可以看到。我宁愿这样做,也不要为同一个活动反复重新创建不同的动作栏。

更新: 这里没有回答的是stacktrace:

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)

2 个答案:

答案 0 :(得分:2)

您可以在onCreateOptionsMenu中轻松启用和停用菜单项。 只需简单地获取您的searchView并放置setEnabled(false),就像这样:

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

我希望这会对你有所帮助。

答案 1 :(得分:1)

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

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

所以试试这个:

mSearchView.setInputType(0x00000000);

并切换回可编辑状态:

mSearchView.setInputType(0x00000001);