您可以使用菜单项中的搜索。转到下面的代码使用这个
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>
让我知道它是否有用。?