Java代码添加RadioButton

  • xml:

         <RadioGroup
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             <RadioButton
                 android:layout_width="0dp"
                 android:layout_height="50dp"
                 android:button="@null"
                 android:background="@null"
                 android:drawableTop="@drawable/selector_home"
                 android:text="button"
                 android:textColor="@color/selector_circle_report_text"
                 android:textSize="16sp"/>
                 ... ...
         </RadioGroup>
    
  • java:
    final int tabNum = 3;
    tabLayout = findViewById(R.id.lay_tab);
    RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(org.xclcharts.common.DensityUtil.getScreenWidth(_context) / tabNum, ViewGroup.LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER;
    for (int i = 0; i < tabNum; i++) {
    final RadioButton rb1 = new RadioButton(_context);
    rb1.setBackgroundResource(android.R.color.transparent);
    rb1.setButtonDrawable(android.R.color.transparent);
    rb1.setLayoutParams(params);
    rb1.setGravity(Gravity.CENTER);
    // 文本颜色选择,引用res-xml
    ColorStateList csl = getResources().getColorStateList(R.color.color_menu_text);
    if (csl != null) {
    rb1.setTextColor(csl);
    }
    rb1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
    StateListDrawable listDrawable = new StateListDrawable();
    switch (i) {
    case 0:
    rb1.setText(R.string.home);
    listDrawable = (StateListDrawable) getResources().getDrawable(R.drawable.selector_home);
    break;
    case 1:
    rb1.setText(R.string.school);
    listDrawable = (StateListDrawable) getResources().getDrawable(R.drawable.selector_work);
    break;
    case 2:
    rb1.setText(R.string.mine);
    listDrawable = (StateListDrawable) getResources().getDrawable(R.drawable.selector_user);
    break;
    }

         final int finalI = i;
         rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                 if (b) {
                     if (finalI == 1 && !MainApplication.isLogin()) {
                         //处理学院未登录点击
                         SharedPreferences sp = getSharedPreferences("XueXi", MODE_PRIVATE);
                         SharedPreferences.Editor edit = sp.edit();
                         edit.putString("xuexi", "1");
                         edit.putString("pdhome", "110");
                         edit.commit();
                         Intent in = new Intent();
                         in.setClass(_context, LoginActivity.class);
                         startActivity(in);
                     } else {
                         showFragment(fragments.get(finalI), "fragment" + finalI);
                         curIndex = finalI;
                     }
                 }
             }
         });
         setRbDrawable(rb1, listDrawable);
         tabLayout.addView(rb1);
     }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值