gpt4 book ai didi

android - 在 Fragment 中添加 onOptionsItemSelected 调用

转载 作者:IT老高 更新时间:2023-10-28 23:37:39 26 4
gpt4 key购买 nike

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//no inspection SimplifiableIfStatement
if (id == R.id.action_filter) {
FragmentManager fm = getSupportFragmentManager();
if (userType.equals("İş Arayan"))
filterDialogTitle = "İş İlanları Filtre";
else if (userType.equals("Hizmet Arayan"))
filterDialogTitle = "Hizmet İlanları Filtre";
FilterDialogFragment editNameDialogFragment = FilterDialogFragment.newInstance(filterDialogTitle);
editNameDialogFragment.show(fm, "fragment_edit_name");
return true;
}

return super.onOptionsItemSelected(item);
}

我在 Fragment 中添加,但它没有被调用,如果我在 MainActivity 中添加,它可以工作,但我想在 Fragment 中调用它。我该怎么做?

最佳答案

Fragment 中你必须调用 setHasOptionsMenu(true)

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

然后假设你必须处理 menu_item_to_handle_in_fragment 项目点击

For Fragment class

  @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {

case R.id.menu_item_to_handle_in_fragment:
// Do onlick on menu action here
return true;
}
return false;
}

For Activity class

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {

case R.id.menu_item_to_handle_in_fragment:
return false;
}
return false;
}

关于android - 在 Fragment 中添加 onOptionsItemSelected 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39316800/

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