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

以前一直以为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;
	@Override
	protected 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() {
			@Override
			public 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() {
			@Override
			public 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() {
			@Override
			public 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);
				}
			}

			// 下面两个方法可以直接无视
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			@Override
			public 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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值