RadioButton_Fragment切换

1、布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.viewpager_fragmentdemo.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rd1"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:background="#000"
                android:button="@null"
                android:checked="true"
                android:gravity="center"
                android:text="首页"
                android:textColor="#fff"
                android:textSize="20sp" />

            <View
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="#fff"></View>

            <RadioButton
                android:id="@+id/rd2"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:background="#000"
                android:button="@null"
                android:gravity="center"
                android:text="发现"
                android:textColor="#fff"
                android:textSize="20sp" />
        </RadioGroup>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:gravity="center">

            <TextView
                android:id="@+id/tv1"
                android:layout_width="70dp"
                android:layout_height="2dp" />

            <TextView
                android:id="@+id/tv2"
                android:layout_width="70dp"
                android:layout_height="2dp" />
        </LinearLayout>

    </LinearLayout>

    <FrameLayout
        android:id="@+id/fl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>

</LinearLayout>

2、MainActivity代码

package com.example.viewpager_fragmentdemo;

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.CompoundButton;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.viewpager_fragmentdemo.fragment.Fragment1;
import com.example.viewpager_fragmentdemo.fragment.Fragment2;

public class MainActivity extends AppCompatActivity {

    private RadioButton rd1;
    private RadioButton rd2;
    private FrameLayout fl;
    private TextView tv1;
    private TextView tv2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rd1 = findViewById(R.id.rd1);
        rd2 = findViewById(R.id.rd2);
        fl = findViewById(R.id.fl);
        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);


        boolean checked = rd1.isChecked();
        if (checked){
            FragmentManager supportFragmentManager1 = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction1 = supportFragmentManager1.beginTransaction();
            fragmentTransaction1.replace(R.id.fl,new Fragment1());
            fragmentTransaction1.commit();
            rd1.setTextColor(getResources().getColor(R.color.colorAccent));
            tv1.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        }


        rd1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setTitleColor();
                FragmentManager supportFragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fl,new Fragment2());
                fragmentTransaction.commit();
                rd2.setTextColor(getResources().getColor(R.color.colorAccent));
                tv2.setBackgroundColor(getResources().getColor(R.color.colorAccent));
            }
        });

        rd2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setTitleColor();
                FragmentManager supportFragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fl,new Fragment1());
                fragmentTransaction.commit();
                rd1.setTextColor(getResources().getColor(R.color.colorAccent));
                tv1.setBackgroundColor(getResources().getColor(R.color.colorAccent));

            }
        });



    }

    public void setTitleColor(){
        rd1.setTextColor(getResources().getColor(R.color.colorWhite));
        rd2.setTextColor(getResources().getColor(R.color.colorWhite));
        tv1.setBackgroundColor(getResources().getColor(R.color.colorWhite));
        tv2.setBackgroundColor(getResources().getColor(R.color.colorWhite));
    }
}
3、建立Fragment省略

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值