Android searchview attribute, Android search box SearchView attribute and usage details

Introduction to SearchView

SearchView is an Android native search box control that provides a user interface for user search queries.

SearchView displays a search icon by default. Click the icon to expand the search box. If you want the search box to expand by default, you can use setIconifiedByDefault(false); to achieve.

SearchView property

b144807b11e254dcc4ebe7dca6ec5608.png

SearchView uses

Define SearchView in xml:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_margin="15dp"

android:orientation="vertical"

tools:context="com.airsaid.searchviewdemo.MainActivity">

android:id="@+id/searchView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:iconifiedByDefault="false"

android:queryHint="Please enter the search content" />

android:id="@+id/listView"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1" />

Main code:

public class MainActivity extends AppCompatActivity {

private String[] mStrs = {"aaa", "bbb", "ccc", "airsaid"};

private SearchView mSearchView;

private ListView mListView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mSearchView = (SearchView) findViewById(R.id.searchView);

mListView = (ListView) findViewById(R.id.listView);

mListView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, mStrs));

mListView.setTextFilterEnabled(true);

// 设置搜索文本监听

mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

// 当点击搜索按钮时触发该方法

@Override

public boolean onQueryTextSubmit(String query) {

return false;

}

// 当搜索内容改变时触发该方法

@Override

public boolean onQueryTextChange(String newText) {

if (!TextUtils.isEmpty(newText)){

mListView.setFilterText(newText);

}else{

mListView.clearTextFilter();

}

return false;

}

});

}

}

Screenshot of the effect:

8a5d53aa19aae31398b325d77370079b.gif

The above is the whole content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support and find a tutorial network.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324156789&siteId=291194637