其他分享
首页 > 其他分享> > android – 在具有MenuItem.SHOW_AS_ACTION_ALWAYS的ActionBarSherlock内向EditText添加TextWatcher

android – 在具有MenuItem.SHOW_AS_ACTION_ALWAYS的ActionBarSherlock内向EditText添加TextWatcher

作者:互联网

我正在使用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).

有人能指出我正确的方向吗?

解决方法:

仅供参考,

这对我有用,在ActionBar的整个宽度上给我EditText(我似乎没有RelativeLayout,即使使用MenuItem.SHOW_AS_ACTION_ALWAYS也是如此)并调用它的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");
    }

    };

&安培;我的layout / search_text:

    <?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>

标签:android,android-edittext,menuitem,actionbarsherlock,textwatcher
来源: https://codeday.me/bug/20190901/1786429.html