Get view of Fragment from FragmentActivity

前端 未结 5 1735
一个人的身影
一个人的身影 2021-01-01 10:09

I have a FragmentActivity where I add a Fragment. Depending on some situation, I want to access Buttons from that fragment\'s layout

5条回答
  •  遥遥无期
    2021-01-01 10:56

    If you want to access your component and set some data, I suggest you to make a method inside the fragment like this:

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    
    public class DetailFragment extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.details, container, false);
            return view;
        }
    
    
        public void setSettings(){
            Button button = (Button) getView().findViewById(R.id.my_button);
            button.setOnClickListener(new MyClickListener());
        }
    }
    

提交回复
热议问题