如何在片段中声明单击侦听按钮以切换到 activity?

How do i declare button on Click listen in a fragment to switch to an activity?

我试过了,但出现错误"Onclick listener can't be applied here"请大家帮帮我

这是我的Fragment.java

public class Fragment extends Fragment {

    public Fragment() {
        // Required empty public constructor
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
        final Button button = (Button) rootView.findViewById(R.id.payment_info);
        final Button button1 = (Button) rootView.findViewById(R.id.redeem);
        final Button button2 = (Button) rootView.findViewById(R.id.make_payment);
        final Button button3 = (Button) rootView.findViewById(R.id.payment_histories);


        // Inflate the layout for this fragment
        return rootView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

OnClickListener 是一个接口,因此您不仅要扩展片段,还要实现 OnClick。

在 class 的声明中试试这个:

public class Fragment extends Fragment implements OnClickListener

希望对您有所帮助。