package com.example.week; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v7.app.AppCompatActivity; import android.widget.FrameLayout; import android.widget.RadioButton; import android.widget.RadioGroup; public class MainActivity extends FragmentActivity { private RadioGroup gr; private RadioButton shouye; private RadioButton xigua; private RadioButton wei; private RadioButton wode; private FrameLayout zhan; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); getSupportFragmentManager().beginTransaction().replace(R.id.zhan,new Fragment1()).commit(); gr.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.shouye: getSupportFragmentManager().beginTransaction(). replace(R.id.zhan,new Fragment1()).commit(); break; case R.id.xigua: getSupportFragmentManager().beginTransaction(). replace(R.id.zhan, new Fragment2()).commit(); break; case R.id.wei: getSupportFragmentManager().beginTransaction(). replace(R.id.zhan,new Fragment3()).commit(); break; case R.id.wode: getSupportFragmentManager().beginTransaction(). replace(R.id.zhan,new Fragment4()).commit(); break; } } }); } private void initView() { gr = (RadioGroup) findViewById(R.id.gr); shouye = (RadioButton) findViewById(R.id.shouye); xigua = (RadioButton) findViewById(R.id.xigua); wei = (RadioButton) findViewById(R.id.wei); wode = (RadioButton) findViewById(R.id.wode); zhan = (FrameLayout) findViewById(R.id.zhan); } }