首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >invalidateOptionsMenu在片段中不起作用

invalidateOptionsMenu在片段中不起作用
EN

Stack Overflow用户
提问于 2014-08-09 14:23:04
回答 1查看 24.8K关注 0票数 24

我想根据编辑文本中的文字显示或隐藏动作栏中的项目

因此,我做了以下工作

            public class NounSearch extends  android.app.Fragment 
{

EditText seachEditText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);
            View rootView = inflater.inflate(R.layout.nounsearchactivity, container, false);
                    //Intiate EditText
            seachEditText =(EditText) rootView.findViewById(R.id.nounSearch);
            seachEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                        searchResult.Entities = new ArrayList<NounModel>();
                        _currentPage = 0;
                        categoryId = -1;
                        new GetNouns().execute();
                        return true;
                    }


                    return false;
                }
            });
            seachEditText.addTextChangedListener(new TextWatcher() {

               public void afterTextChanged(Editable s) {
               }

               public void beforeTextChanged(CharSequence s, int start,
                 int count, int after) {
               }

               public void onTextChanged(CharSequence s, int start,
                 int before, int count) {
                 getActivity().invalidateOptionsMenu();
               }
              });

    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);

            if(seachEditText.getText().toString().length() > 0)
            {
                menu.findItem(R.id.action_search).setVisible(true);
            }
            else
            {
                menu.findItem(R.id.action_search).setVisible(false);
            }
    }





}

但是actionitem永远不会出现

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25215853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档