我想执行与以下链接相同的功能:
http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/
但是这个例子使用了普通的EditText,我不想浪费屏幕空间。因此,我更喜欢使用位于ActionBar内部的EditText,与用于正常搜索的相同。如何访问此EditText以在其上放置TextWatcher?它的ID是什么?
我想添加这个TextWatcher:
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
提前致谢!
答案 0 :(得分:0)
您可以使用菜单项搜索。转到下面的代码 使用此
EditText search;
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout rl =(RelativeLayout)inflater.inflate(R.layout.search_text, null);
search=(EditText)rl.getChildAt(0);
search.addTextChangedListener(filterTextWatcher);
menu.add(0, 1, 1, R.string.inlineSearch).setIcon(R.drawable.action_search).setActionView(rl).setShowAsA ction(MenuItem.S HOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
}
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
Log.e("TextWatcher","after");
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.e("TextWatcher","before");
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e("TextWatcher","onText");
}
};
和搜索布局是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="fill_horizontal"
>
<EditText
android:id="@+id/search_text"
style="@style/EditTextHoloDark"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dip"
android:imeActionId="1337"
android:imeOptions="actionDone"
android:inputType="textCapCharacters|textNoSuggestions"
android:singleLine="true"
android:hint="Search"
android:textStyle="bold"
android:drawableLeft="@drawable/action_search"
/>
</RelativeLayout>
让我知道它是否有用。?