简体   繁体   English

片段的菜单项 onClickListener

[英]Menu Item onClickListener for fragments

I have the following menu:我有以下菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    app:showAsAction="never"
    android:title="Settings" />

<item android:id="@+id/menu_share"
    android:icon="@drawable/ic_heart_outline_white_24dp"
    app:showAsAction="always"
    android:title="Favorite" />
</menu>

How would I set an onClick listener for this?我将如何为此设置一个 onClick 侦听器? bearing in mind that my class extends Fragment:请记住,我的课程扩展了 Fragment:

public class My_Profile extends Fragment implements ....{}

...and I do not know if this makes any difference but I have already inflated a layout in this java file too. ...我不知道这是否有什么不同,但我也已经在这个 java 文件中膨胀了一个布局。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

myView = inflater.inflate(R.layout.testFragment, container, false);
...
}

Update 1更新 1

This is what I tried after the suggestions but nothing seems to happen can someone please tell me what im doing wrong:这是我在提出建议后尝试过的,但似乎没有发生任何事情,有人可以告诉我我做错了什么:

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


 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.menu_share) {
            Log.d("ButtonClick", "button click worked");
        }

        return super.onOptionsItemSelected(item);
    }

and in the onCreate added this:并在 onCreate 添加了这个:

setHasOptionsMenu(true);

Update 2更新 2

Dont know if this makes any difference, I'm just working with an example at the moment and the menu is also inflated like this.不知道这是否有什么不同,我目前只是在处理一个例子,菜单也像这样膨胀。 I dont know if that affects the other 2 methods:我不知道这是否会影响其他两种方法:

 mToolbar.inflateMenu(R.menu.menu_main);

update 3更新 3

This is now what I have tried yet, still the button click doesn't log anything:这是我现在尝试过的,但单击按钮仍然没有记录任何内容:

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable  Bundle savedInstanceState) {

        setHasOptionsMenu(true);

        myView = inflater.inflate(R.layout.testFragment, container, false);

        ...

        return myView;
        }

and just for testing redirected to one of my other Activities, yet nothing still happens:并且只是为了测试重定向到我的其他活动之一,但仍然没有任何反应:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.menu_share) {
            Intent myIntent = new Intent(getActivity(),LoginActivity.class);
            startActivity(myIntent);
        }else{
            Intent myIntent = new Intent(getActivity(),SignupActivity.class);
            startActivity(myIntent);
        }

        return super.onOptionsItemSelected(item);
    }


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

Your fragment should have these two methods to handle the menu: onCreateOptionsMenu and onOptionsItemSelected with the onClick stuff in the latter.您的片段应该有这两种方法来处理菜单: onCreateOptionsMenuonOptionsItemSelected以及后者中的 onClick 内容。

For example:例如:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        //TODO
    } else if (id == R.id.menu_share) {
        //TODO
    }

    return super.onOptionsItemSelected(item);
}

EDIT编辑

Don't forget setHasOptionsMenu(true) for your fragment (thanks)不要忘记setHasOptionsMenu(true)为您的片段(谢谢)

Well, I think that you have a good code.好吧,我认为你有一个很好的代码。 All you need to do is re-arrange the onCreateOptionsMenu(...)您需要做的就是重新排列onCreateOptionsMenu(...)

For instance:例如:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    return inflater.inflate(R.layout.testFragment, container, false);
}

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

I hope this helps.我希望这会有所帮助。

@BadCodersHub i have tried your scenario and its working fine with following code. @BadCodersHub 我已经尝试了您的场景,并且使用以下代码可以正常工作。

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    View myView = inflater.inflate(R.layout.fragment_main, container, false);
    return myView;
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.action_settings:
            Log.d("","action Settings Clicked !!");
            break;
        case R.id.menu_share:
            Log.d("","action menu_share Clicked !!");
            break;
        default:
            return super.onOptionsItemSelected(item);
    }

    return super.onOptionsItemSelected(item);
}

Hope this will help you.you need to override onCreateOptionMenu and onOptionItemSelected methods希望这会帮助你。你需要覆盖 onCreateOptionMenu 和 onOptionItemSelected 方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM