Google 致力于为黑人社区推动种族平等。查看具体举措

SearchView

public class SearchView
extends LinearLayoutCompat implements CollapsibleActionView

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ androidx.appcompat.widget.LinearLayoutCompat
         ↳ androidx.appcompat.widget.SearchView


A widget that provides a user interface for the user to enter a search query and submit a request to a search provider. Shows a list of query suggestions or results, if available, and allows the user to pick a suggestion or result to launch into.

When the SearchView is used in an ActionBar as an action view, it's collapsed by default, so you must provide an icon for the action.

If you want the search field to always be visible, then call setIconifiedByDefault(false).

Developer Guides

For information about using SearchView, read the Search API guide. Additional information about action views is also available in the <Action Bar API guide

Summary

Nested classes

interface SearchView.OnCloseListener

 

interface SearchView.OnQueryTextListener

Callbacks for changes to the query text. 

interface SearchView.OnSuggestionListener

Callback interface for selection events on suggestions. 

Inherited constants

Inherited fields

Public constructors

SearchView(Context context)
SearchView(Context context, AttributeSet attrs)
SearchView(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

void clearFocus()
int getImeOptions()

Returns the IME options set on the query text field.

int getInputType()

Returns the input type set on the query text field.

int getMaxWidth()

Gets the specified maximum width in pixels, if set.

CharSequence getQuery()

Returns the query string currently in the text field.

CharSequence getQueryHint()

Returns the hint text that will be displayed in the query text field.

CursorAdapter getSuggestionsAdapter()

Returns the adapter used for suggestions, if any.

boolean isIconfiedByDefault()

Returns the default iconified state of the search field.

boolean isIconified()

Returns the current iconified state of the SearchView.

boolean isQueryRefinementEnabled()

Returns whether query refinement is enabled for all items or only specific ones.

boolean isSubmitButtonEnabled()

Returns whether the submit button is enabled when necessary or never displayed.

void onActionViewCollapsed()

Called when this view is collapsed as an action view.

void onActionViewExpanded()

Called when this view is expanded as an action view.

void onWindowFocusChanged(boolean hasWindowFocus)
boolean requestFocus(int direction, Rect previouslyFocusedRect)
void setIconified(boolean iconify)

Iconifies or expands the SearchView.

void setIconifiedByDefault(boolean iconified)

Sets the default or resting state of the search field.

void setImeOptions(int imeOptions)

Sets the IME options on the query text field.

void setInputType(int inputType)

Sets the input type on the query text field.

void setMaxWidth(int maxpixels)

Makes the view at most this many pixels wide R.attr.maxWidth

void setOnCloseListener(SearchView.OnCloseListener listener)

Sets a listener to inform when the user closes the SearchView.

void setOnQueryTextFocusChangeListener(View.OnFocusChangeListener listener)

Sets a listener to inform when the focus of the query text field changes.

void setOnQueryTextListener(SearchView.OnQueryTextListener listener)

Sets a listener for user actions within the SearchView.

void setOnSearchClickListener(View.OnClickListener listener)

Sets a listener to inform when the search button is pressed.

void setOnSuggestionListener(SearchView.OnSuggestionListener listener)

Sets a listener to inform when a suggestion is focused or clicked.

void setQuery(CharSequence query, boolean submit)

Sets a query string in the text field and optionally submits the query as well.

void setQueryHint(CharSequence hint)

Sets the hint text to display in the query text field.

void setQueryRefinementEnabled(boolean enable)

Specifies if a query refinement button should be displayed alongside each suggestion or if it should depend on the flags set in the individual items retrieved from the suggestions provider.

void setSearchableInfo(SearchableInfo searchable)

Sets the SearchableInfo for this SearchView.

void setSubmitButtonEnabled(boolean enabled)

Enables showing a submit button when the query is non-empty.

void setSuggestionsAdapter(CursorAdapter adapter)

You can set a custom adapter if you wish.

Protected methods

void onDetachedFromWindow()
void onLayout(boolean changed, int left, int top, int right, int bottom)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void onRestoreInstanceState(Parcelable state)
Parcelable onSaveInstanceState()

Inherited methods

Public constructors

SearchView

public SearchView (Context context)

Parameters
context Context

SearchView

public SearchView (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

SearchView

public SearchView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

clearFocus

public void clearFocus ()

getImeOptions

public int getImeOptions ()

Returns the IME options set on the query text field.

Returns
int the ime options

getInputType

public int getInputType ()

Returns the input type set on the query text field.

Returns
int the input type R.attr.inputType

getMaxWidth

public int getMaxWidth ()

Gets the specified maximum width in pixels, if set. Returns zero if no maximum width was specified.

Returns
int the maximum width of the view R.attr.maxWidth

getQuery

public CharSequence getQuery ()

Returns the query string currently in the text field.

Returns
CharSequence the query string

getQueryHint

public CharSequence getQueryHint ()

Returns the hint text that will be displayed in the query text field.

The displayed query hint is chosen in the following order:

  1. Non-null value set with setQueryHint(CharSequence)
  2. Value specified in XML using app:queryHint
  3. Valid string resource ID exposed by the SearchableInfo via SearchableInfo.getHintId()
  4. Default hint provided by the theme against which the view was inflated

Returns
CharSequence the displayed query hint text, or null if none set R.attr.queryHint

getSuggestionsAdapter

public CursorAdapter getSuggestionsAdapter ()

Returns the adapter used for suggestions, if any.

Returns
CursorAdapter the suggestions adapter

isIconfiedByDefault

public boolean isIconfiedByDefault ()

Returns the default iconified state of the search field.

Returns
boolean R.attr.iconifiedByDefault

isIconified

public boolean isIconified ()

Returns the current iconified state of the SearchView.

Returns
boolean true if the SearchView is currently iconified, false if the search field is fully visible.

isQueryRefinementEnabled

public boolean isQueryRefinementEnabled ()

Returns whether query refinement is enabled for all items or only specific ones.

Returns
boolean true if enabled for all items, false otherwise.

isSubmitButtonEnabled

public boolean isSubmitButtonEnabled ()

Returns whether the submit button is enabled when necessary or never displayed.

Returns
boolean whether the submit button is enabled automatically when necessary

onActionViewCollapsed

public void onActionViewCollapsed ()

Called when this view is collapsed as an action view. See MenuItem.collapseActionView().

onActionViewExpanded

public void onActionViewExpanded ()

Called when this view is expanded as an action view. See MenuItem.expandActionView().

onWindowFocusChanged

public void onWindowFocusChanged (boolean hasWindowFocus)

Parameters
hasWindowFocus boolean

requestFocus

public boolean requestFocus (int direction, 
                Rect previouslyFocusedRect)

Parameters
direction int

previouslyFocusedRect Rect

Returns
boolean

setIconified

public void setIconified (boolean iconify)

Iconifies or expands the SearchView. Any query text is cleared when iconified. This is a temporary state and does not override the default iconified state set by setIconifiedByDefault(boolean). If the default state is iconified, then a false here will only be valid until the user closes the field. And if the default state is expanded, then a true here will only clear the text field and not close it.

Parameters
iconify boolean: a true value will collapse the SearchView to an icon, while a false will expand it.

setIconifiedByDefault

public void setIconifiedByDefault (boolean iconified)

Sets the default or resting state of the search field. If true, a single search icon is shown by default and expands to show the text field and other buttons when pressed. Also, if the default state is iconified, then it collapses to that state when the close button is pressed. Changes to this property will take effect immediately.

The default value is true.

Parameters
iconified boolean: whether the search field should be iconified by default R.attr.iconifiedByDefault

setImeOptions

public void setImeOptions (int imeOptions)

Sets the IME options on the query text field.

Parameters
imeOptions int: the options to set on the query text field R.attr.imeOptions

setInputType

public void setInputType (int inputType)

Sets the input type on the query text field.

Parameters
inputType int: the input type to set on the query text field R.attr.inputType

setMaxWidth

public void setMaxWidth (int maxpixels)

Makes the view at most this many pixels wide R.attr.maxWidth

Parameters
maxpixels int

setOnCloseListener

public void setOnCloseListener (SearchView.OnCloseListener listener)

Sets a listener to inform when the user closes the SearchView.

Parameters
listener SearchView.OnCloseListener: the listener to call when the user closes the SearchView.

setOnQueryTextFocusChangeListener

public void setOnQueryTextFocusChangeListener (View.OnFocusChangeListener listener)

Sets a listener to inform when the focus of the query text field changes.

Parameters
listener View.OnFocusChangeListener: the listener to inform of focus changes.

setOnQueryTextListener

public void setOnQueryTextListener (SearchView.OnQueryTextListener listener)

Sets a listener for user actions within the SearchView.

Parameters
listener SearchView.OnQueryTextListener: the listener object that receives callbacks when the user performs actions in the SearchView such as clicking on buttons or typing a query.

setOnSearchClickListener

public void setOnSearchClickListener (View.OnClickListener listener)

Sets a listener to inform w