Android搜索框searchView的使用

Android搜索框searchView的使用_第1张图片
activity_main.xml 文件

    
                                                               
    






MainActivity.java 文件

 private void initView() {
    mSear = (SearchView) findViewById(R.id.sear);
    mLi = (ListView) findViewById(R.id.li);
    mLi.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,name));
    //是否自动缩小为图标
    mLi.setTextFilterEnabled(true);
    //显示默认文字
    mSear.setQueryHint("查钊");
    mSear.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            //点击搜索时
            Toast.makeText(MainActivity.this,"输入了",Toast.LENGTH_SHORT ).show();
        return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if(newText!=null){
                mLi.setFilterText(newText);
            }else{
                mLi.clearTextFilter();
            }
            return true;
        }
    });
}

效果 ,点击搜索安妞
Android搜索框searchView的使用_第2张图片

你可能感兴趣的:(Android搜索框searchView的使用)