package com.example.searchviewapplication;
/*API生单词: submit 提交 ,递交 query 询问,查询 iconify 图标化,图符化 collapsible :可折叠菜单项
*
*
* <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<SearchView //定义一个搜索框,一个listView
android:id="@+id/sv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</SearchView>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></ListView>
</LinearLayout>
*
*
*
*/
import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.SearchView.OnQueryTextListener;
public class MainActivity extends Activity implements SearchView.OnQueryTextListener{ //实现查询事件监听
private SearchView sv;
private ListView lv;
private final String[] mStrings={"aaaaaaa","bbbbbbb","cccccccc"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.lv);
//定义List里的内容,通过new ArrayAdapter《String》()提供内容
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mStrings));
lv.setTextFilterEnabled(true); //定义过滤器
sv=(SearchView)findViewById(R.id.sv);
sv.setIconifiedByDefault(false); //定义搜索框图标化
sv.setOnQueryTextListener((OnQueryTextListener) this); // 绑定事件监听器
sv.setSubmitButtonEnabled(true); //提供搜索按钮
sv.setQueryHint("搜索");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
if(TextUtils.isEmpty(newText)){
lv.clearTextFilter();
}else{
lv.setFilterText(newText);
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
Toast toast=Toast.makeText(getApplicationContext(), "您的选择是: "+query, Toast.LENGTH_SHORT);//返回值toast
toast.setGravity(Gravity.CENTER, 0, 0);//设置吐司为居中
toast.show();
return true;
}
}