gpt4 book ai didi

android - invalidateOptionsMenu 在 fragment 中不起作用

转载 作者:IT老高 更新时间:2023-10-28 23:02:04 24 4
gpt4 key购买 nike

我想根据编辑文本中的文本是否显示或隐藏操作栏中的项目

所以我做了以下

            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永远不会出现

最佳答案

要更新 fragment 中的 onCreateOptionsMenu,您需要在 fragment 的 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);


}

关于android - invalidateOptionsMenu 在 fragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25215853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com