FrameLayout+RadioGroup、RadioButton 点击按钮跳转页面

package baway.com.mv.view.activity;


import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.List;

import baway.com.mv.R;
import baway.com.mv.view.fragment.Fragment1;
import baway.com.mv.view.fragment.Fragment2;
import baway.com.mv.view.fragment.Fragment3;
import baway.com.mv.view.fragment.Fragment4;

/**
 * 类的作用:
 * 类的思路:
 * 作者:岳俊飞
 * 时间:2017/5/11
 */

public class Zhuactivity extends BaseActivity  {
    private List<Fragment> list;
    private RadioGroup rg;
    private ViewPager vp;
    private RadioButton shouye;
    private RadioButton shipin;
    private RadioButton toutiao;
    private RadioButton denglu;
    private Fragment[] mFragments;
    private int mIndex=0;
    private FrameLayout fr;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhu_item);
        initView();
        initFragment();
        initState();

    }
   
    private void initFragment() {
        Fragment1 fragment1 = new Fragment1();
        Fragment2 fragment2 = new Fragment2();
        Fragment3 fragment3 = new Fragment3();
        Fragment4 fragment4 = new Fragment4();
        mFragments = new Fragment[]{fragment1,fragment2,fragment3,fragment4};
        FragmentTransaction ft =getSupportFragmentManager().beginTransaction();
        ft.add(R.id.content,fragment1).commit();
        setIndexSelected(0);
    }
    private void setIndexSelected(int index) {

        if(mIndex==index){
            return;
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft= fragmentManager.beginTransaction();
        //隐藏
        ft.hide(mFragments[mIndex]);
        //判断是否添加
        if(!mFragments[index].isAdded()){
            ft.add(R.id.content,mFragments[index]).show(mFragments[index]);
        }else {
            ft.show(mFragments[index]);
        }
        ft.commit();
        //再次赋值
        mIndex=index;

    }

    private void initView() {
        rg = (RadioGroup) findViewById(R.id.rg);
        fr = (FrameLayout) findViewById(R.id.content);
        shouye = (RadioButton) findViewById(R.id.shouye);
        shipin = (RadioButton) findViewById(R.id.shipin);
        toutiao = (RadioButton) findViewById(R.id.weitoutiao);
        denglu = (RadioButton) findViewById(R.id.weidenglu);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.shouye:
                        setIndexSelected(0);
                        break;
                    case R.id.shipin:
                        setIndexSelected(1);
                        break;
                    case R.id.weitoutiao:
                        setIndexSelected(2);
                        break;
                    case R.id.weidenglu:
                        setIndexSelected(3);
                        break;
                }
            }
        });
    }
   
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,RadioGroup是一个Android视图组件,它通常用于提供一组单选按钮。它可以让用户从几个选项中选择一个选项。 要实现页面跳转,您需要在RadioGroup添加单选按钮,然后将每个单选按钮与相应的Activity关联。 以下是一个简单的示例,它使用RadioGroupRadioButton来实现页面跳转: 1.在您的layout文件中添加RadioGroupRadioButton组件。例如: ``` <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Page 1"/> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Page 2"/> </RadioGroup> ``` 2.在您的Activity中获取RadioGroup的引用,然后为每个RadioButton添加点击事件处理程序。在每个处理程序中,使用Intent对象将用户重定向到所选Activity。例如: ``` RadioGroup radioGroup = findViewById(R.id.radioGroup); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { Intent intent; switch (checkedId) { case R.id.radioButton1: intent = new Intent(MainActivity.this, Page1Activity.class); startActivity(intent); break; case R.id.radioButton2: intent = new Intent(MainActivity.this, Page2Activity.class); startActivity(intent); break; default: break; } } }); ``` 在上面的代码中,我们为RadioGroup设置了一个监听器,当用户单击单选按钮时,onCheckedChanged()方法将被调用。在这个方法中,我们使用switch语句将用户重定向到所选Activity。 这就是使用RadioGroup实现页面跳转的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值