首页 > 代码库 > 安卓学习-界面-ui-SearchView
安卓学习-界面-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; } }); }}
安卓学习-界面-ui-SearchView
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。