Java – number of clicks to hear the activity button from within the clip

I just started learning fragments. I have an activity, that is, 1000 lines of code, which I have converted into fragments

I wonder if there is any simple way to allow my fragment to listen for buttons residing in the XML of its parent activity I tried to add a regular listener in the oncreateview of the fragment, which contains the ID of the button, but it (expected) will give me a null pointer exception

I know the onsearchclicklistener method to do this, but it may take a lot of time to manually refactor to achieve my needs (at least from the usage I can say, I'm glad to make mistakes), so I wonder if anyone knows any other method to do this in a non repetitive way

Solution

You can try to get the View reference in the activity in this way:

View view = getActivity().findViewById(R.id.view_in_activity);

Then add a listener:

view.setOnClickListener(new OnClickListener() {
    void onClick(View v) {
        // Perform some action
    }
})
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>