1

我想执行与以下链接相同的功能:

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                          
}
});

提前致谢!

4

1 回答 1

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>

让我知道它是否有用。?

于 2013-10-07T12:05:16.853 回答