2015-11-06 29 views
6

我有一个搜索视图,我有一个查询提示属性。但是只有在点击搜索视图后才会显示提示。有没有办法让它在被点击之前出现?SearchView查询提示之前单击它

<SearchView 
     android:id="@+id/searchView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentStart="true" 
     android:layout_below="@+id/textView4" 
     android:layout_marginBottom="14dp" 
     android:layout_marginTop="30dp" 
     android:queryHint="Search" /> 

enter image description here

enter image description here

我已经看到了类似的查询另一个SO问题,但它并没有解决。这就是为什么我再次问。

+1

因为它处于“iconified”状态 – Selvin

+0

在这里我的解决方案:https://stackoverflow.com/questions/46681500/android-4-3-not-focus-searchview-not-show-hint?noredirect= 1#comment80355499_46681500 – Alex

回答

7

你可以添加这些行;

SearchView searchView = (SearchView) findViewById(R.id.searchView); 
    searchView.onActionViewExpanded(); 
    searchView.setIconified(true); 
+4

刚刚检查过,并在xml iconifiedByDefault中找到了一个属性。我将它设置为false并且它工作。谢谢。 :) – nootnoot

+3

searchView.setIconified(false);为我工作。谢谢:) – SimplyProgrammer

+0

searchView.setIconified(false); - 焦点searchView和结果显示提示。但是当searchView不在焦点时,我需要显示提示。我如何做到这一点? – Alex

0

化妆setIconified(假的)对我来说 这样的作品,但是当它是未点击,再次提示 - 即工作消失

-2

在XML中,您可以使用此 机器人:queryHint = “搜索”

+1

请在答案中加入更多信息(一个完整的例子,一个有价值的链接)。 –

0

这为我工作:

searchView.onActionViewExpanded(); 
new Handler().postDelayed(new Runnable() 
{ 
    @Override 
    public void run() { 
      searchView.clearFocus(); 
    } 
}, 300); 

而且,不要忘记这些添加到manifest.xml中:

<intent-filter> 
    <action android:name="android.intent.action.SEARCH"/> 
    <action android:name="android.intent.action.MAIN" /> 

    <category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter>