Android Fragments invalidateOptionsMenu在片段中无效

ekqde3dh  于 2023-08-06  发布在  Android
关注(0)|答案(3)|浏览(91)

根据EditText中是否有文本显示或隐藏ActionBar中的项目。

所以,我做了以下的事情

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从未出现。我如何才能做到这一点?

5gfr0r5j

5gfr0r5j1#

要更新片段中的onCreateOptionsMenu,需要在片段的onCreate方法中调用setHasOptionsMenu(true);。否则在调用getActivity().invalidateOptionsMenu();时将无法更新它

样本:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

字符串
编辑:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
    if(seachEditText.getText().toString().length() > 0) {
        menu.findItem(R.id.action_search).setVisible(true);
    }
    else {
        menu.findItem(R.id.action_search).setVisible(false);
    }
    super.onCreateOptionsMenu(menu, inflater);
}

pxiryf3j

pxiryf3j2#

试试这个:

@Override
public boolean onPrepareOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onPrepareOptionsMenu(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);
    }
}

字符串

j2qf4p5b

j2qf4p5b3#

方案一

这是一个片段的工作

getActivity().invalidateOptionsMenu();

字符串

方案二

如果上面的代码不起作用,请尝试此操作

invalidateOptionsMenu(getActivity());


导入此库

import static androidx.core.app.ActivityCompat.invalidateOptionsMenu;

相关问题