本文将为您提供关于Android控件RadioButton实现多选一功能的详细介绍,我们还将为您解释android多项选择控件的相关知识,同时,我们还将为您提供关于AndroidRadioButton
本文将为您提供关于Android控件RadioButton实现多选一功能的详细介绍,我们还将为您解释android多项选择控件的相关知识,同时,我们还将为您提供关于Android RadioButton、Android RadioButton 控件、Android RadioButton 背景图变形问题、android radiobutton 设置默认选中后无法实现单选的实用信息。
本文目录一览:- Android控件RadioButton实现多选一功能(android多项选择控件)
- Android RadioButton
- Android RadioButton 控件
- Android RadioButton 背景图变形问题
- android radiobutton 设置默认选中后无法实现单选
Android控件RadioButton实现多选一功能(android多项选择控件)
RadioButton实现多选一功能的方法,具体内容如下
一、简介
二、RadioButton实现多选一方法
1、将多个RadioButton放在一个RadioGroup里面
<RadioGroup android:id="@+id/radioGroup1" android:layout_width="match_parent" android:layout_height="wrap_content" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:textColor="#FFFFFF" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:textColor="#FFFFFF" /> </RadioGroup>
2、在RadioGroup里面取出每个RadioButton
public void onClick(View v) { // Todo Auto-generated method stub int len = radioGroup1.getChildCount(); for (int i = 0; i < len; i++) { RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);11 } }
3、检查每个RadioButton是否被选取
if (radio.isChecked()) { break; }
4、取出被选取的那个RadioButton里面的值
Toast.makeText(Activity01.this,radio.getText(),Toast.LENGTH_LONG).show();
三、代码实例
效果图:
代码:
fry.Activity01
package fry; import com.example.RadioButtonDemo1.R; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; public class Activity01 extends Activity { private Button btn_chooseGender; private RadioGroup radioGroup1; private TextView tv_answer; @Override protected void onCreate(Bundle savedInstanceState) { // Todo Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity01); btn_chooseGender = (Button) findViewById(R.id.btn_chooseGender); radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1); tv_answer = (TextView) findViewById(R.id.tv_answer); /* * RadioButton实现多选一方法 * 1、将多个RadioButton放在一个RadioGroup里面 * 2、在RadioGroup里面取出每个RadioButton * 3、检查每个RadioButton是否被选取 * 4、取出被选取的那个RadioButton里面的值 */ btn_chooseGender.setonClickListener(new OnClickListener() { @Override public void onClick(View v) { // Todo Auto-generated method stub int len = radioGroup1.getChildCount(); for (int i = 0; i < len; i++) { RadioButton radio = (RadioButton) radioGroup1.getChildAt(i); if (radio.isChecked()) { Toast.makeText(Activity01.this,Toast.LENGTH_LONG).show(); break; } } } }); } }
/RadioButtonDemo1/res/layout/activity01.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="性别" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_gravity="center_horizontal" android:textColor="#FFFFFF" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="match_parent" android:layout_height="wrap_content" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:textColor="#FFFFFF" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:textColor="#FFFFFF" /> </RadioGroup> <Button android:id="@+id/btn_chooseGender" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选择性别" android:textColor="#FFFFFF" /> /> <TextView android:id="@+id/tv_answer" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_gravity="center_horizontal" android:textColor="#FFFFFF" /> </LinearLayout>
四、收获
1、
android:textColor="#FFFFFF"
设置颜色,直接用#FFFFFF
2、
android:layout_gravity="center_horizontal"
文字居中显示
3、
RadioButton在RadioGroup里面实现多选一
4、
android:background="@android:color/black"
设置黑色,系统自带颜色
5、
int len = radioGroup1.getChildCount();
RadioGroup获取孩子数量
6、
RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);
RadioGroup获取孩子
7、
if (radio.isChecked())
判断RadioButton是否被选取
8、
Toast.makeText(Activity01.this,Toast.LENGTH_LONG).show();
吐司
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
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 控件
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 背景图变形问题
问题描述:
一般我们在做收藏、关注功能的时候会使用到RadioButton,但是我们在设置背景为图片的时候,会出现变形拉伸,或者挤压。
解决方法:
通过尝试发现:设置RadioButton的text属性,只需要有这个属性就可以,然后再添加textsize属性,将字体大小属性值设置为比较小,我设置为2sp。运行后我们会发现图片变形问题不复存在。
分析:
没有看源代码,在这里意淫一下,意淫的就当玩乐吧!可能是个小bug,RadioButton 的背景图会受到字体大小的影响,当字体大于背景图的大小就会出现变形的情况。
通过设置字体的大小小于背景图的大小,这样RadioButton的大小取决于较大的那个元素也就是背景图,这样背景图和RadioButton的大小保持一致就避免了背景图变形。
android radiobutton 设置默认选中后无法实现单选
通过radiogroup+radiobutton实现单选的功能的时候加上默认选中后无法实现单选。经过查资料发现原来是没有设置radiobutton的id导致出现改bug。
关于Android控件RadioButton实现多选一功能和android多项选择控件的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Android RadioButton、Android RadioButton 控件、Android RadioButton 背景图变形问题、android radiobutton 设置默认选中后无法实现单选的相关知识,请在本站寻找。
本文标签: