android edittext + listview 实现搜索listview中的内容

来源:互联网 发布:数据录入项目外包 编辑:程序博客网 时间:2024/04/30 17:33

以前一直以为edittext中输入一些东西.然后可以检测listview中的内容很高大上.一直没有去尝试.现在项目中遇到了.特此过来尝试一番.结果发现挺简单的,效果还不错,主要就是用到了edittext的 textchange监听 以及listview的过滤.下面直接上截图:





xml:

<LinearLayout 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"    android:orientation="vertical"    tools:context="com.example.testedittextlistview.MainActivity" >    <include layout="@layout/myedittext" />    <ListView        android:id="@+id/lv_main"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#000" >    </ListView></LinearLayout>

activity:

package com.example.testedittextlistview;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.text.Editable;import android.text.TextWatcher;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.WindowManager;import android.view.View.OnClickListener;import android.view.inputmethod.InputMethodManager;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.EditText;import android.widget.ImageButton;import android.widget.ListView;import android.widget.Toast;public class MainActivity extends Activity {private EditText query;private ImageButton search_clear;private ListView lv_main;//控制输入框显示隐藏的代码private InputMethodManager inputMethodManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {inputMethodManager=  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);query = (EditText) findViewById(R.id.query);search_clear = (ImageButton) findViewById(R.id.search_clear);search_clear.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//点击一下清空内容咯query.getText().clear();hideSoftKeyboard();//隐藏键盘}});//listview初始化控件并丰富一些内容lv_main = (ListView) findViewById(R.id.lv_main);final String[] myString = new String[] { "aaaa", "bbbb", "cccc","dddd", "i love you" };final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,myString);lv_main.setAdapter(adapter);// 为listview添加点击事件lv_main.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {// 这里注意使用的是adapter.getItem(position)// ,而不是myString[position],如果使用第二种的话,效果很坑哟Toast.makeText(getApplicationContext(),"点击了" + adapter.getItem(position), Toast.LENGTH_SHORT).show();}});// 为edittext添加内容改变的监听,onTextChanged 里面让adapter,设置过滤的条件query.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count) {adapter.getFilter().filter(s);if(s.length()>0) { search_clear.setVisibility(View.VISIBLE);} else {search_clear.setVisibility(View.GONE);}}// 下面两个方法可以直接无视@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void afterTextChanged(Editable s) {}});}/** * 隐藏输入法的示例代码 */void hideSoftKeyboard() {if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {if (getCurrentFocus() != null)inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);}}}


0 0
原创粉丝点击