zoukankan      html  css  js  c++  java
  • 安卓学习-界面-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>
    View Code

    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;
                }
            });
            
        }
    }
    View Code
  • 相关阅读:
    自我介绍
    汇编实验报告五
    汇编第二章知识总结
    汇编第一章知识总结
    汇编实验报告四
    汇编实验报告三
    汇编实验报告(二)
    汇编实验报告(一)
    实验报告(七)
    实验报告(六)
  • 原文地址:https://www.cnblogs.com/weijj/p/3972853.html
Copyright © 2011-2022 走看看