radiogroup 订阅
RadioGroup 展开全文
RadioGroup
信息
操作系统
iOS
开发语言
Objective-C
开源协议
Apache
RadioGroup
RadioGroup 模仿 C# 的 RadioGroup 实现,对于RadioGroup内所有RadioBox自动单选,IOS7风格,点击选择的动态效果不错。可使用可视化方式编辑,也可以完全代码控制,简单方便。
收起全文
精华内容
下载资源
问答
  • RadioGroup

    2016-07-14 02:03:36
    RadioGroup

    RadioGroup

    展开全文
  • Radiogroup

    2015-05-11 10:02:27
    Radiogroup,Radiobutton实现底部选择
  • radiogroup

    2013-10-14 16:13:34
    radiogroup中包括两个radiobutton,切换两个radiobutton实现不同操作
  • radioGroup

    2019-10-04 20:15:35
    package com.example.radiogroup_06; import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.Menu;import android.widget.RadioButton;import android.widg...

    package com.example.radiogroup_06;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.Menu;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {

    private TextView textView;
    private RadioGroup radioGroup;
    private RadioButton radioBtn01;
    private RadioButton radioBtn02;
    private RadioButton radioBtn03;
    private RadioButton radioBtn04;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.text);
    radioGroup = (RadioGroup) findViewById(R.id.radio_group);
    radioBtn01 = (RadioButton) findViewById(R.id.radio_btn01);
    radioBtn03 = (RadioButton) findViewById(R.id.radio_btn02);
    radioBtn03 = (RadioButton) findViewById(R.id.radio_btn03);
    radioBtn04 = (RadioButton) findViewById(R.id.radio_btn04);

    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    public void onCheckedChanged(RadioGroup group,int checkedId){
    if(checkedId==radioBtn03.getId()){
    displayToast("正確答案:"+radioBtn03.getText()+"\n"+"恭喜您!回答正確");
    }else{
    displayToast("回答錯誤");
    }
    }


    });
    }


    public void displayToast(String string){
    Toast toast = Toast.makeText(this, string, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.BOTTOM, 0, 220);
    toast.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    }

     

    XML文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text" />
    <RadioGroup
    android:id="@+id/radio_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton
    android:id="@+id/radio_btn01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/radio_btn01"/>
    <RadioButton
    android:id="@+id/radio_btn02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/radio_btn02"/>
    <RadioButton
    android:id="@+id/radio_btn03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/radio_btn03"/>
    <RadioButton
    android:id="@+id/radio_btn04"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/radio_btn04"/>
    </RadioGroup>

    </LinearLayout>

     

    转载于:https://www.cnblogs.com/Smart-Du/p/4302000.html

    展开全文
  • 自定义RadioGroup

    2016-09-09 18:24:48
    自定义RadioGroup
  • Android | RadioGroup--设置默认选中

    万次阅读 2016-12-06 17:53:09
    RadioGroup 中, 是通过RadioButton 的 id 来控制是否选中。1. 布局文件中控制:如果在xml 布局文件中需要控制一个RadioButton 默认选中,就需要给他设置一个id。如果不设置id 的话,就会导致该RadioButton 一直是...

    RadioGroup 中, 是通过RadioButton 的 id 来控制是否选中。

    ###1. 布局文件中控制:
    如果在xml 布局文件中需要控制一个RadioButton 默认选中,就需要给他设置一个id。如果不设置id 的话,就会导致该RadioButton 一直是选中状态。代码如下:

     <RadioGroup
            android:id="@+id/rg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            
        <!--该RadioButton 需要设置默认选中,所以给出id-->    
        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selector_bk_rb"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:padding="10dp"
    
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selector_bk_rb"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="第2 个button"/>
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selector_bk_rb"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="第3 个button"/>
    

    2. 代码控制

    很多时候,我们需要在代码中动态的 添加 RadioButton 到RadioGroup ,这时候,如果想设置某个RadioButton 的默认选中,就需要先通过 getId( ) 获取 radioButton 的id,然后再去设置。方式也有两种,一种是通过id 获取view 后去设置;一种是使用 radiogroup.check( id ) 直接设置。具体如下:

            RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rg_cus);
    
            for (int i = 0; i < 12; i++) {
                RadioButton radioButton = new RadioButton(getContext());
                radioButton.setButtonDrawable(null);
                radioButton.setBackgroundResource(R.drawable.selector_bk_rb);
                radioButton.setText("Button" + i);
                radioButton.setPadding(15, 15, 15, 15);
                radioButton.setTextSize(20);
                radioGroup.addView(radioButton);
    
                if (i == 0) {
                    //                // 设置默认选中方式1 ,先获取控件,然后设置选中               
                    //                //根据id 获取radioButton 控件
                    //                RadioButton rb_checked = (RadioButton) radioGroup.findViewById(radioButton.getId());
                    //                //设置默认选中
                    //                rb_checked.setChecked(true);
    
                    // 设置默认选中方式2                
                    radioGroup.check(radioButton.getId());
                }
            }
    
    展开全文
  • import android.content.Context; import android.util.AttributeSet;...import android.widget.RadioGroup; public class FlowRadioGroup extends RadioGroup { public FlowRadioGroup(Context con.

     

    不同尺寸都展示良好

     

    布局如下

     <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp"
                android:fontFamily="sans-serif-thin"
                android:orientation="vertical"
                app:cardCornerRadius="10dp"
                app:cardPreventCornerOverlap="true"
                app:cardUseCompatPadding="true"
                app:contentPadding="10dp">
    
    
                <com.anguomob.constellation.view.FlowRadioGroup
                    android:id="@+id/rg_fourtune"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
    
    
                    <RadioButton
                        android:layout_marginTop="20dp"
                        android:layout_marginStart="20dp"
                        android:id="@+id/rb_love_fortune"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:background="@drawable/fortune_select"
                        android:button="@null"
                        android:gravity="center"
                        android:paddingStart="10dp"
                        android:paddingTop="5dp"
                        android:paddingEnd="10dp"
                        android:paddingBottom="5dp"
                        android:text="@string/love_fortune"
                        android:textColor="@color/fortune_color" />
    
    
                    <RadioButton
                        android:layout_marginTop="20dp"
                        android:layout_marginStart="20dp"
                        android:id="@+id/rb_today_fortune"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:background="@drawable/fortune_select"
                        android:button="@null"
                        android:checked="true"
                        android:gravity="center"
                        android:paddingStart="10dp"
                        android:paddingTop="5dp"
                        android:paddingEnd="10dp"
                        android:paddingBottom="5dp"
                        android:text="@string/toady_fortune"
                        android:textColor="@color/fortune_color" />
    
    
                    <RadioButton
                        android:layout_marginTop="20dp"
                        android:layout_marginStart="20dp"
                        android:id="@+id/rb_tomorrow_fortune"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:background="@drawable/fortune_select"
                        android:button="@null"
                        android:gravity="center"
                        android:paddingStart="10dp"
                        android:paddingTop="5dp"
                        android:paddingEnd="10dp"
                        android:paddingBottom="5dp"
                        android:text="@string/tomorrow_fortune"
                        android:textColor="@color/fortune_color" />
    
    
                    <RadioButton
                        android:layout_marginTop="20dp"
                        android:layout_marginStart="20dp"
                        android:id="@+id/rb_week_fortune"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:background="@drawable/fortune_select"
                        android:button="@null"
                        android:gravity="center"
                        android:paddingStart="10dp"
                        android:paddingTop="5dp"
                        android:paddingEnd="10dp"
                        android:paddingBottom="5dp"
                        android:text="@string/week_fortune"
                        android:textColor="@color/fortune_color" />
    
    
                    <RadioButton
                        android:layout_marginTop="20dp"
                        android:layout_marginStart="20dp"
                        android:id="@+id/rb_mounth_fortune"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:background="@drawable/fortune_select"
                        android:button="@null"
                        android:gravity="center"
                        android:paddingStart="10dp"
                        android:paddingTop="5dp"
                        android:paddingEnd="10dp"
                        android:paddingBottom="5dp"
                        android:text="@string/mounth_fortune"
                        android:textColor="@color/fortune_color" />
    
    
                    <RadioButton
                        android:layout_marginTop="20dp"
                        android:layout_marginStart="20dp"
                        android:id="@+id/rb_year_fortune"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
    
                        android:background="@drawable/fortune_select"
                        android:button="@null"
                        android:gravity="center"
                        android:paddingStart="10dp"
                        android:paddingTop="5dp"
                        android:paddingEnd="10dp"
                        android:paddingBottom="5dp"
                        android:text="@string/year_fortune"
                        android:textColor="@color/fortune_color" />
    
    
                </com.anguomob.constellation.view.FlowRadioGroup>
    
    
            </androidx.cardview.widget.CardView>

     样式代码如下

    fortune_select.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:drawable="@drawable/shape_fortune_checked" android:state_checked="true"/>
     <item android:drawable="@drawable/shape_fortune_defalue"/>
    </selector>

     shape_fortune_checked.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="20dp" />
        <solid android:color="@color/color_main" />
    </shape>

    shape_fortune_defalue.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="20dp" />
        <solid android:color="@color/colorWhite" />
        <stroke android:width="1dp" android:color="@color/color_main"/>
    </shape>

    颜色如下

    <color name="colorWhite">#fff</color>
    <color name="color_main">#1F2121</color>

     背景颜色如下

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorWhite" android:state_checked="true"/>
    <item android:color="@color/colorWhite" android:state_selected="true"/>
    <item android:color="@color/color_main" />
    </selector>

    自定义view代码如下

    package com.anguomob.constellation.view;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.RadioGroup;
    
    public class FlowRadioGroup extends RadioGroup {
        public FlowRadioGroup(Context context) {
            super(context);
        }
    
        public FlowRadioGroup(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    
            //调用ViewGroup的方法,测量子view
            measureChildren(widthMeasureSpec, heightMeasureSpec);
    
            //最大的宽
            int maxWidth = 0;
            //累计的高
            int totalHeight = 0;
    
            //当前这一行的累计行宽
            int lineWidth = 0;
            //当前这行的最大行高
            int maxLineHeight = 0;
            //用于记录换行前的行宽和行高
            int oldHeight;
            int oldWidth;
    
            int count = getChildCount();
            //假设 widthMode和heightMode都是AT_MOST
            for (int i = 0; i < count; i++) {
                View child = getChildAt(i);
                MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
                //得到这一行的最高
                oldHeight = maxLineHeight;
                //当前最大宽度
                oldWidth = maxWidth;
    
                int deltaX = child.getMeasuredWidth() + params.leftMargin + params.rightMargin;
                if (lineWidth + deltaX + getPaddingLeft() + getPaddingRight() > widthSize) {//如果折行,height增加
                    //和目前最大的宽度比较,得到最宽。不能加上当前的child的宽,所以用的是oldWidth
                    maxWidth = Math.max(lineWidth, oldWidth);
                    //重置宽度
                    lineWidth = deltaX;
                    //累加高度
                    totalHeight += oldHeight;
                    //重置行高,当前这个View,属于下一行,因此当前最大行高为这个child的高度加上margin
                    maxLineHeight = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;
    
                } else {
                    //不换行,累加宽度
                    lineWidth += deltaX;
                    //不换行,计算行最高
                    int deltaY = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;
                    maxLineHeight = Math.max(maxLineHeight, deltaY);
                }
                if (i == count - 1) {
                    //前面没有加上下一行的搞,如果是最后一行,还要再叠加上最后一行的最高的值
                    totalHeight += maxLineHeight;
                    //计算最后一行和前面的最宽的一行比较
                    maxWidth = Math.max(lineWidth, oldWidth);
                }
            }
            //加上当前容器的padding值
            maxWidth += getPaddingLeft() + getPaddingRight();
            totalHeight += getPaddingTop() + getPaddingBottom();
            setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : maxWidth,
                    heightMode == MeasureSpec.EXACTLY ? heightSize : totalHeight);
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            int count = getChildCount();
            //pre为前面所有的child的相加后的位置
            int preLeft = getPaddingLeft();
            int preTop = getPaddingTop();
            //记录每一行的最高值
            int maxHeight = 0;
            for (int i = 0; i < count; i++) {
                View child = getChildAt(i);
                MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
                //r-l为当前容器的宽度。如果子view的累积宽度大于容器宽度,就换行。
                if (preLeft + params.leftMargin + child.getMeasuredWidth() + params.rightMargin + getPaddingRight() > (r - l)) {
                    //重置
                    preLeft = getPaddingLeft();
                    //要选择child的height最大的作为设置
                    preTop = preTop + maxHeight;
                    maxHeight = getChildAt(i).getMeasuredHeight() + params.topMargin + params.bottomMargin;
                } else { //不换行,计算最大高度
                    maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + params.topMargin + params.bottomMargin);
                }
                //left坐标
                int left = preLeft + params.leftMargin;
                //top坐标
                int top = preTop + params.topMargin;
                int right = left + child.getMeasuredWidth();
                int bottom = top + child.getMeasuredHeight();
                //为子view布局
                child.layout(left, top, right, bottom);
                //计算布局结束后,preLeft的值
                preLeft += params.leftMargin + child.getMeasuredWidth() + params.rightMargin;
            }
        }
    }
    

    代码动态创建呢?

            rgFourtune.removeAllViews();
            for (element in elements) {
                val radioButton = RadioButton(this)
                radioButton.setText(element.text())
                radioButton.setButtonDrawable(null)
                radioButton.background = ContextCompat.getDrawable(this, R.drawable.fortune_select)
                radioButton.gravity = Gravity.CENTER
      
                radioButton.setTextColor(ContextCompat.getColor(this, if (radioButton.isChecked) R.color.white else R.color.color_main))
    
    
                val params = RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                val dp20 = DensityUtil.dp2px(20f);
                val dp10 = DensityUtil.dp2px(10f);
                val dp5 = DensityUtil.dp2px(5f);
                params.setMargins(dp20, dp20, 0, dp20)
                radioButton.layoutParams = params;
                radioButton.setPadding(dp10, dp5, dp10, dp5)
                radioButton.setOnClickListener {
                    initNetWorkData(ApiConstants.API_ORIGIN_HOST + element.attr("href"));
                }
                rgFourtune.addView(radioButton)
    
            }

     

    展开全文
  • RadioGroup的父控件是:LinearLayoutRadioGroup是一个单选按钮组,RadioGroup 可将各自不同的RadioButton ,在RadioGroup 中的各个radiobutton是互斥的,也就是说,这些radiogroup中单选按钮只能选中一个,当一个被...
  • 重构RadioGroup

    2015-08-29 17:59:08
    重构RadioGroup,实现多行多列RidioButton
  • RadioGroup validator

    2021-01-08 12:29:32
    <div><p>It would be nice to allow RadioGroup validation with saripaar.</p><p>该提问来源于开源项目:ragunathjawahar/android-saripaar</p></div>
  • 替换RadioGroup

    2015-12-10 13:11:33
    * 实现类似RadioGroup的效果 * * 需要设置单选的控件,id统一命名成:mp+数字(为了方便自动查找); * 然后在所在的Activity调用MyRadioGroup.getInstance.autoFindView()

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 5,707
精华内容 2,282
关键字:

radiogroup