3

我正在使用 ActionBarSherlock,我想用 MenuItem.SHOW_AS_ACTION_ALWAYS 在其中显示一个 EditText。它占据栏的整个宽度并根据需要显示。但是我在尝试向其中添加 TextWatcher 时遇到了麻烦。

所以根据这里接受的答案:http: //goo.gl/ite6h我写:

    EditText search;
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {

    menu.add(0, 1, 1, R.string.inlineSearch).setIcon(R.drawable.action_search).setActionView(search).setShowAsAction(MenuItem.SHOW_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");
    }

}; 

@Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
        Log.e("onOptions",""+item.getItemId()); // Line 150
        switch (item.getItemId()) {
            case 1:
                search = (EditText) item.getActionView();
                search.addTextChangedListener(filterTextWatcher);
                search.requestFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        }  
        return true;
    }  

问题是,永远不会到达第 150 行(没有日志),并且没有任何侦听器未添加到搜索中(EditText)。

谁能指出我正确的方向?

4

1 回答 1

5

供参考,

这对我有用,给了我整个 ActionBar 宽度的 EditText(即使使用 MenuItem.SHOW_AS_ACTION_ALWAYS,我似乎也无法做到没有 RelativeLayout)并调用它的 TextWatcher 方法:

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).setShowAsAction(MenuItem.SHOW_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>
于 2012-08-20T19:19:57.823 回答