单选框RadioGroup,单选按钮RadioButton的使用
2017-06-07 22:30
465 查看
RadioButton是最普通的UI组件之一,继承了Button类,可以直接使用Button支持的各种属性和方法。
RadioButton与普通按钮不同的是,它多了一个可以选中的功能,可额外指定一个Android:checked属性,该属性可以指定初始状态时是否被选中,其实也可以不用指定,默认初始状态都不选中。
使用RadioButton必须和单选框RadioGroup一起使用,在RadioGroup中放置RadioButton,通过setOnCheckedChangeListener( )来响应按钮的事件;
下面是一个实例:
![](https://img-blog.csdn.net/20170607222817413?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWURDb29raWU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
XML代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/iv_main_image"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rg_main_group">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1号佳丽"
android:ems="3"
android:id="@+id/rb_main_one"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2号佳丽"
android:ems="3"
android:id="@+id/rb_main_two"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3号佳丽"
android:ems="3"
android:id="@+id/rb_main_three"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4号佳丽"
android:ems="3"
android:id="@+id/rb_main_four"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5号佳丽"
android:ems="3"
android:id="@+id/rb_main_five"/>
</RadioGroup>
</RelativeLayout>
</LinearLayout>
这里RadioButton的android:checked属性可以多个都选择为true,但运行之后只会选择最后一个checked属性作为初始状态。
很多初学者都会遇到一个问题:程序代码(包括xml文件)均无错误提示,但是在设备上运行时候却出错,其中一个原因就是布局或者组件没有指定layout_width和layout_height属性,导致运行出错!
Java代码
public class MainActivity extends AppCompatActivity {
private ImageView iv_main_image;
private RadioGroup rg_main_group;
int currentIndex=0;
File files[];
private Map<String, Bitmap> m;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//根据ID找到该图片控件
iv_main_image = (ImageView) findViewById(R.id.iv_main_image);
m = new HashMap<>();
//手机是否有内存卡
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//获取路径
String sdCardPath=Environment.getExternalStorageDirectory().getAbsolutePath();
File file=new File(sdCardPath+"/Images");
files= file.listFiles();
Toast.makeText(this,""+sdCardPath,Toast.LENGTH_LONG).show();
Bitmap bm=BitmapFactory.decodeFile(files[0].getAbsolutePath());
iv_main_image.setImageBitmap(bm);
RadioButton rb_main_one= (RadioButton) findViewById(R.id.rb_main_one);
rb_main_one.setChecked(true);
}
int i=1;
for (File file : files) {
m.put(i+"号佳丽",BitmapFactory.decodeFile(file.getAbsolutePath()));
i++;
}
//根据ID找到RadioGroup实例
rg_main_group = (RadioGroup) this.findViewById(R.id.rg_main_group);
//绑定一个匿名监听器
rg_main_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton rb= (RadioButton) findViewById(checkedId);
String s=rb.getText().toString();
iv_main_image.setImageBitmap(m.get(s));
}
});
}
}
RadioButton与普通按钮不同的是,它多了一个可以选中的功能,可额外指定一个Android:checked属性,该属性可以指定初始状态时是否被选中,其实也可以不用指定,默认初始状态都不选中。
使用RadioButton必须和单选框RadioGroup一起使用,在RadioGroup中放置RadioButton,通过setOnCheckedChangeListener( )来响应按钮的事件;
下面是一个实例:
XML代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/iv_main_image"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rg_main_group">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1号佳丽"
android:ems="3"
android:id="@+id/rb_main_one"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2号佳丽"
android:ems="3"
android:id="@+id/rb_main_two"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3号佳丽"
android:ems="3"
android:id="@+id/rb_main_three"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4号佳丽"
android:ems="3"
android:id="@+id/rb_main_four"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5号佳丽"
android:ems="3"
android:id="@+id/rb_main_five"/>
</RadioGroup>
</RelativeLayout>
</LinearLayout>
这里RadioButton的android:checked属性可以多个都选择为true,但运行之后只会选择最后一个checked属性作为初始状态。
很多初学者都会遇到一个问题:程序代码(包括xml文件)均无错误提示,但是在设备上运行时候却出错,其中一个原因就是布局或者组件没有指定layout_width和layout_height属性,导致运行出错!
Java代码
public class MainActivity extends AppCompatActivity {
private ImageView iv_main_image;
private RadioGroup rg_main_group;
int currentIndex=0;
File files[];
private Map<String, Bitmap> m;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//根据ID找到该图片控件
iv_main_image = (ImageView) findViewById(R.id.iv_main_image);
m = new HashMap<>();
//手机是否有内存卡
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//获取路径
String sdCardPath=Environment.getExternalStorageDirectory().getAbsolutePath();
File file=new File(sdCardPath+"/Images");
files= file.listFiles();
Toast.makeText(this,""+sdCardPath,Toast.LENGTH_LONG).show();
Bitmap bm=BitmapFactory.decodeFile(files[0].getAbsolutePath());
iv_main_image.setImageBitmap(bm);
RadioButton rb_main_one= (RadioButton) findViewById(R.id.rb_main_one);
rb_main_one.setChecked(true);
}
int i=1;
for (File file : files) {
m.put(i+"号佳丽",BitmapFactory.decodeFile(file.getAbsolutePath()));
i++;
}
//根据ID找到RadioGroup实例
rg_main_group = (RadioGroup) this.findViewById(R.id.rg_main_group);
//绑定一个匿名监听器
rg_main_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton rb= (RadioButton) findViewById(checkedId);
String s=rb.getText().toString();
iv_main_image.setImageBitmap(m.get(s));
}
});
}
}
相关文章推荐
- 安卓编程——单选框RadioGroup,单选按钮RadioButton的使用
- 单选框RadioGroup,单选按钮RadioButton的使用
- RadioGroup、RadioButton(单选按钮)、CheckBox(多选按钮)和Toast的使用方法
- Android程序开发中单选按钮(RadioGroup)的使用详解
- 单选按钮RadioGroup和RadioButton
- React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)
- 使用RadioGroup与RadioButton实现单选效果
- android单选按钮选择,RadioGroup,radioButton
- android单选按钮RadioGroup的详细使用
- RadioButton单选按钮实现RadioGroup.OnCheckedChangeListener
- Android 使用RadioGroup和RadioButton实现单选效果
- 山寨开机选择四大系统-----------RadioGroup 、RadioButton单选按钮
- 单选按钮RadioGroup的使用
- 使用RadioButton和RadioGroup实现多种多样的单选情况
- Android入门(八):使用RadioGroup 和RadioButton组件建立单选清单
- 安卓控件之单选按钮 (RadioButton和RadioGroup)
- Android基础总结八:单选框RadioGroup,RadioButton的使用
- Android控件笔记——使用RadioGroup和RadioButton实现单选效果
- android基本控件示例RadioButton单选按钮,一般和RadioGroup一起用(03)
- 单选按钮RadioGroup和RadioButton