Button Listener for button in fragment in android

后端 未结 9 2102
北恋
北恋 2020-12-02 11:04

I am new to Android and trying to learn on my own. But I am having a tough time with Fragments. I am creating a simple application to learn fragments. I think it may seem si

9条回答
  •  無奈伤痛
    2020-12-02 11:09

    You only have to get the view of activity that carry this fragment and this could only happen when your fragment is already created

    override the onViewCreated() method inside your fragment and enjoy its magic :) ..

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Button button = (Button) view.findViewById(R.id.YOURBUTTONID);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             //place your action here
             }
        });
    

    Hope this could help you ;

提交回复
热议问题