我正在使用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" 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 – 在具有MenuItem.SHOW_AS_ACTION_ALWAYS的ActionBarSherlock内向EditText添加TextWatcher全部内容,希望文章能够帮你解决android – 在具有MenuItem.SHOW_AS_ACTION_ALWAYS的ActionBarSherlock内向EditText添加TextWatcher所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)