Fragment实现RadioButton切换

Fragment实现RadioButton切换

 

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!!!!!


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值