GVKun编程网logo

Android RadioButton使用(android中radiobutton控件)

15

本篇文章给大家谈谈AndroidRadioButton使用,以及android中radiobutton控件的知识点,同时本文还将给你拓展AndroidRadioButton、AndroidRadioB

本篇文章给大家谈谈Android RadioButton使用,以及android中radiobutton控件的知识点,同时本文还将给你拓展Android RadioButton、Android RadioButton 控件、Android RadioButton 背景图变形问题、android radioButton动态切换图片等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Android RadioButton使用(android中radiobutton控件)

Android RadioButton使用(android中radiobutton控件)

例题2-12

activity_main.xml代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <GridLayout android:layout_width="fill_parent"
 3     android:layout_height="fill_parent"
 4     android:columnCount="4"
 5     android:rowCount="6"
 6     xmlns:android="http://schemas.android.com/apk/res/android">
 7     <RadioGroup>
 8         <RadioButton android:id="@+id/boy"
 9             android:layout_width="wrap_content"
10             android:layout_height="wrap_content"
11             android:checked="true"
12             android:text="男"/>
13         <RadioButton android:id="@+id/girl"
14             android:layout_width="wrap_content"
15             android:layout_height="wrap_content"
16             android:text="女"/>
17     </RadioGroup>
18     <Button android:id="@+id/but1"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:text="确认"
22         android:layout_columnSpan="4"/>
23     <TextView android:id="@+id/text1"
24         android:layout_height="wrap_content"
25         android:layout_width="wrap_content"
26         android:text="" />
27 </GridLayout>

mainactivity.java代码

 1 package com.example.hello;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.os.Bundle;
 6 import android.telephony.SmsManager;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.CheckBox;
10 import android.widget.ProgressBar;
11 import android.widget.RadioButton;
12 import android.widget.TextView;
13 
14 public class MainActivity extends AppCompatActivity {
15     Button but1;
16     TextView textView;
17     RadioButton radioButton1,radioButton2;
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         textView = (TextView)findViewById(R.id.text1);
23         but1 = (Button)findViewById(R.id.but1);
24         radioButton1 = (RadioButton)findViewById(R.id.boy);
25         radioButton2 = (RadioButton)findViewById(R.id.girl);
26         but1.setonClickListener(new View.OnClickListener() {
27             @Override
28             public void onClick(View v) {
29                 String s="";
30                 if(radioButton1.isChecked())
31                     s=s+radioButton1.getText();
32                 if (radioButton2.isChecked())
33                     s=s+radioButton2.getText();
34                 textView.setText("性别:"+s);
35             }
36         });
37 
38     }
39 
40 }

 

Android RadioButton

Android RadioButton

1.RadioGroup继承于LinearLayout

    <RadioGroup >

        <RadioButton/>

        ...

    </RadioGroup>

 

((RadioButton) radioGroup.getChildAt(index)).setChecked(true);

2.监听器

    RadioGroup.OnCheckedChangeListener{        
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int id) {
            RadioButton rb = radioGroup.findViewById(id);
            int checkedIndex = radioGroup.indexOfChild(rb);
        }

    }

3.自定义自动换行RadioGroup

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;

public class MyRadioGroup extends RadioGroup {
    private static final String TAG = "RadioGroupEx";

    public MyRadioGroup(Context context) {
        super(context);
    }

    public MyRadioGroup(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;
                Log.v(TAG, "maxHeight:" + totalHeight + "---" + "maxWidth:" + maxWidth);

            } 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;
        }
    }
}

Android RadioButton 控件

Android RadioButton 控件

 

RadioButton   单选按钮  

 

常用属性:

text  文本

checked=“true”  默认选中

 

一组互斥的单选按钮要放在 RadioGroup 中。RadioGroup 常用属性:

orientation  该组单选按钮的排列方向。

 

示例:

<RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/gender"
            android:orientation="horizontal">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/male"
                android:text="男"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/female"
                android:text="女"/>

</RadioGroup>

 

Android RadioButton 背景图变形问题

Android RadioButton 背景图变形问题


问题描述: 

                    一般我们在做收藏、关注功能的时候会使用到 RadioButton,但是我们在设置背景为图片的时候,会出现变形拉伸,或者挤压。


解决方法:

   通过尝试发现:设置 RadioButton 的 text 属性,只需要有这个属性就可以,然后再添加 textsize 属性,将字体大小属性值设置为比较小,我设置为 2sp。运行后我们会发现图片变形问题不复存在。


   分析:

   没有看源代码,在这里意淫一下,意淫的就当玩乐吧!可能是个小 bug,RadioButton 的背景图会受到字体大小的影响,当字体大于背景图的大小就会出现变形的情况。

通过设置字体的大小小于背景图的大小,这样 RadioButton 的大小取决于较大的那个元素也就是背景图,这样背景图和 RadioButton 的大小保持一致就避免了背景图变形。

android radioButton动态切换图片

android radioButton动态切换图片

1、属性设置

在res/values/attrs.xml定义属性文件

<resources>
    <declare-styleable name="ImageRadioButton">
        <attr name="drawableSize" format="dimension"/>
        <attr name="drawableTop" format="reference"/>

</declare-styleable>
</resources>

2、LayOut xml配置文件

xmlns:attRadoi="http://schemas.android.com/apk/res/com.my.test.pro"

定义属性的命令空间,com.my.test.pro为工程的包定义

定义自定义的属性如下:

<com.my.test.pro.view.ImageRadioButton
            android:id="@+id/mytab_bt_test"
           
            android:text="TEST"
            attRadoi:drawableSize="28dp"
            attRadoi:drawableTop="@drawable/main_selector_test"
            android:drawableTop="@drawable/main_selector_test"
</com.my.test.pro.view.ImageRadioButton>

3、源码示例

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RadioButton;

public class ImageRadioButton extends RadioButton {
    private int mDrawableSize;// xml文件中设置的大小

    public ImageRadioButton(Context context) {
        this(context, null, 0);
    }

    public ImageRadioButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImageRadioButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        //Auto-generated constructor stub
        Drawable drawableLeft = null, drawableTop = null, drawableRight = null, drawableBottom = null;
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.ImageRadioButton);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            Log.i("ImageRadioButton", "attr:" + attr);
            switch (attr) {
                case R.styleable.ImageRadioButton_drawableSize:
                    mDrawableSize = a.getDimensionPixelSize(R.styleable.ImageRadioButton_drawableSize, 50);
                    Log.i("ImageRadioButton", "mDrawableSize:" + mDrawableSize);
                    break;
                case R.styleable.ImageRadioButton_drawableTop:
                    drawableTop = a.getDrawable(attr);
                    break;
                case R.styleable.ImageRadioButton_drawableBottom:
                    drawableRight = a.getDrawable(attr);
                    break;
                case R.styleable.ImageRadioButton_drawableRight:
                    drawableBottom = a.getDrawable(attr);
                    break;
                case R.styleable.ImageRadioButton_drawableLeft:
                    drawableLeft = a.getDrawable(attr);
                    break;
                default :
                    break;
            }
        }
        a.recycle();
        setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
    }

    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left,
                                                        Drawable top, Drawable right, Drawable bottom) {
        if (left != null) {
            left.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        if (right != null) {
            right.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        if (top != null) {
            top.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        if (bottom != null) {
            bottom.setBounds(0, 0, mDrawableSize, mDrawableSize);
        }
        setCompoundDrawables(left, top, right, bottom);
    }
}

关于Android RadioButton使用android中radiobutton控件的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Android RadioButton、Android RadioButton 控件、Android RadioButton 背景图变形问题、android radioButton动态切换图片等相关知识的信息别忘了在本站进行查找喔。

本文标签: