| |||
---|---|---|---|
main布局文件中的布局: <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9.5" android:id="@+id/fl"> </FrameLayout> <RadioGroup android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.5" android:id="@+id/rg" android:orientation="horizontal" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/but1" android:gravity="center" android:button="@null" android:text="首页"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:button="@null" android:id="@+id/but2" android:text="主页"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:button="@null" android:id="@+id/but3" android:text="超市"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:button="@null" android:id="@+id/but4" android:text="小仓库"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:button="@null" android:id="@+id/but5" android:text="尾页"/> </RadioGroup> 代码为:
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.widget.RadioGroup; public class Second extends FragmentActivity { private RadioGroup rg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); rg = findViewById(R.id.rg); //得到管理者 FragmentManager manager=getSupportFragmentManager(); FragmentTransaction beginTransaction = manager.beginTransaction(); beginTransaction.replace(R.id.fl,new Fragment01()); beginTransaction.commit(); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i) { case R.id.but1: FragmentManager manager1 = getSupportFragmentManager(); FragmentTransaction beginTransaction1 = manager1.beginTransaction(); beginTransaction1.replace(R.id.fl, new Fragment01()); beginTransaction1.commit(); break; case R.id.but2: FragmentManager manager2 = getSupportFragmentManager(); FragmentTransaction beginTransaction2 = manager2.beginTransaction(); beginTransaction2.replace(R.id.fl, new Fragment02()); beginTransaction2.commit(); break; case R.id.but3: FragmentManager manager3 = getSupportFragmentManager(); FragmentTransaction beginTransaction3 = manager3.beginTransaction(); beginTransaction3.replace(R.id.fl, new Fragment03()); beginTransaction3.commit(); break; case R.id.but4: FragmentManager manager4 = getSupportFragmentManager(); FragmentTransaction beginTransaction4 = manager4.beginTransaction(); beginTransaction4.replace(R.id.fl, new Fragment04()); beginTransaction4.commit(); break; case R.id.but5: FragmentManager manager5 = getSupportFragmentManager(); FragmentTransaction beginTransaction5 = manager5.beginTransaction(); beginTransaction5.replace(R.id.fl, new Fragment05()); beginTransaction5.commit(); break; } } }); } }程序界的大牛们加油O!!!!!
|
Fragment实现RadioButton切换
最新推荐文章于 2019-11-25 18:59:27 发布