安卓学习-界面-ui-SearchView
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:orientation="vertical" > <SearchView android:id="@+id/searchView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </SearchView> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:background="#888" android:layout_height="wrap_content" > </ListView> </LinearLayout> </RelativeLayout>
MainActivity.java
public class MainActivity extends Activity { SearchView searchView1; ListView listView1; String[] items=new String[]{ "aaaaaaaaa","bbbbbbbbb","cccccccccc", "aa","bb","cc" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); searchView1=(SearchView)findViewById(R.id.searchView1); //显示提交按钮 searchView1.setSubmitButtonEnabled(true); //设置为取则隐藏为只显示一个图标 searchView1.setIconifiedByDefault(false); //提示hint searchView1.setQueryHint("请输入过滤条件"); listView1=(ListView)findViewById(R.id.listView1); //绑定数据集 listView1.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,items)); //设置允许过滤 listView1.setTextFilterEnabled(true); //增加事件 searchView1.setOnQueryTextListener(new OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { Toast.makeText(getApplicationContext(), "你查询的是:"+query, Toast.LENGTH_SHORT).show(); return true; } @Override public boolean onQueryTextChange(String newText) { if(TextUtils.isEmpty(newText)){ listView1.clearTextFilter(); }else{ listView1.setFilterText(newText); } return true; } }); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。