public class MainActivity extends FragmentActivity {
private RadioGroup group;
private Fragmentone fragmentone;
private FragmentTwo fragmentTwo;
private Fragmentthree fragmentthree;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源ID
group = (RadioGroup) findViewById(R.id.group);
fragmentone = new Fragmentone();
fragmentTwo = new FragmentTwo();
fragmentthree = new Fragmentthree();
setfragment(fragmentone);
//设置监听
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.but1:
setfragment(fragmentone);
break;
case R.id.but2:
setfragment(fragmentTwo);
break;
case R.id.but3:
setfragment(fragmentthree);
break;
default:
break;
}
}
});
}
//加载Fragment的方法
public void setfragment(Fragment fragment){
//1.得到Fragment管理者
FragmentManager supportFragmentManager = getSupportFragmentManager();
//2.打开事物
FragmentTransaction beginTransaction = supportFragmentManager.beginTransaction();
//加载Fragment arg0,占位布局 arg1Fragment
beginTransaction.replace(R.id.fragment, fragment);
beginTransaction.commit();
}
}