2015-11-06 58 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>