3

ActionBarSherlock을 사용 중입니다. MenuItem.SHOW_AS_ACTION_ALWAYS를 사용하여 EditText를 표시하려고합니다. 그것은 막대의 전체 너비를 소요하고 원하는대로 보여줍니다. 하지만 TextWatcher를 추가하는 데 문제가 있습니다. 그래서 여기 허용 대답에 따라MenuItem.SHOW_AS_ACTION_ALWAYS가있는 ActionBarSherlock 내에서 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)은 (더 로그)에 도달하지 않습니다와 아무것도 청취자는 검색 (글고 치기)에 추가하지 않습니다.

누구나 올바른 방향으로 나를 가리킬 수 있습니까? 참고로

답변

5

, 이것은 나를 위해 일한

는 (심지어 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"); 
    } 

    }; 

& 내 레이아웃/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> 
+0

는 "EditTextHoloDark"테마 -' @ 스타일에 대한 코드를 게시하시기 바랍니다 수/글고 치기 HoloDark' – Wesley

+0

나를 위해 일했습니다. 감사! – dmon

관련 문제