以前一直以为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);
}
}
}