public class Main2Activity extends AppCompatActivity implements View.OnClickListener { private RadioButton zuixin; private RadioButton zhuanlan; private RadioButton remen; private RadioButton zhuti; private RadioGroup rg; private FrameLayout fl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); initView(); Fragment1 F1=new Fragment1(); getSupportFragmentManager().beginTransaction().replace(R.id.fragment,F1).commit(); zuixin.setOnClickListener(this); zhuanlan.setOnClickListener(this); remen.setOnClickListener(this); zhuti.setOnClickListener(this); } private void initView() { zuixin = (RadioButton) findViewById(R.id.zuixin); zhuanlan = (RadioButton) findViewById(R.id.zhuanlan); remen = (RadioButton) findViewById(R.id.remen); zhuti = (RadioButton) findViewById(R.id.zhuti); rg = (RadioGroup) findViewById(R.id.rg); fl = (FrameLayout) findViewById(R.id.fragment); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.zuixin: Fragment1 F1 = new Fragment1(); getSupportFragmentManager().beginTransaction().replace(R.id.fragment, F1).commit(); break; case R.id.zhuanlan: Fragment2 F2 = new Fragment2(); getSupportFragmentManager().beginTransaction().replace(R.id.fragment, F2).commit(); break; case R.id.remen: Fragment3 F3 = new Fragment3(); getSupportFragmentManager().beginTransaction().replace(R.id.fragment, F3).commit(); break; case R.id.zhuti: Fragment4 F4 = new Fragment4(); getSupportFragmentManager().beginTransaction().replace(R.id.fragment, F4).commit(); break; } } }
fragment和radiobutton
最新推荐文章于 2021-12-31 21:38:30 发布