安卓RadioButton,RadioGroup,Button按钮的使用
来源:互联网 发布:淘宝上有正品阿迪吗 编辑:程序博客网 时间:2024/05/28 05:14
在安卓中,单选按钮和复选按钮都继承至普通按钮,因此他们都可以使用普通按钮支持的各种属性和方法。与普通按钮不同的是,单选和复选按钮提供了可选择的功能。本片先介绍单选按钮及单选按钮组的使用。
这里我会将RadioButton和RadioGroup以及Button和TextView搭配使用。这样便于比较异同,下面直接上代码。
布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/content_main" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.myapplication.MainActivity" tools:showIn="@layout/app_bar_main"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical|center_horizontal" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性别:" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_toRightOf="@id/textView1" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:checked="true" /> <!-- android:checked="true"默认设置为选中状态 --> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup> <Button android:id="@+id/button1" android:layout_toRightOf="@id/radioGroup1" android:text="提交" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:id="@+id/textView2" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="100dp" /></RelativeLayout>
效果图:
JAVA代码:
private RadioGroup radioGroup; private RadioButton radioButton1; private RadioButton radioButton2; private Button button; private TextView textView;
radioGroup=(RadioGroup)findViewById(R.id.radioGroup1); radioButton1=(RadioButton)findViewById(R.id.radio1); radioButton2=(RadioButton)findViewById(R.id.radio2); button=(Button)findViewById(R.id.button1); textView=(TextView)findViewById(R.id.textView2); //在改变单选按钮组的时候获取其值 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton=(RadioButton)findViewById(checkedId); textView.append("group"+radioButton.getText()); } }); //单击其他按钮时获取 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i=0;i<radioGroup.getChildCount();i++){ RadioButton radioButton=(RadioButton)radioGroup.getChildAt(i); if (radioButton.isChecked()){ textView.append("Button"+radioButton.getText()); break; } } } });
0 0
- 安卓RadioButton,RadioGroup,Button按钮的使用
- 安卓编程——单选框RadioGroup,单选按钮RadioButton的使用
- Button的使用(七):RadioGroup、RadioButton
- 安卓RadioGroup和RadioButton简单使用
- 安卓控件之单选按钮 (RadioButton和RadioGroup)
- radioGroup动态加载radiobutton且radiobutton没有button圆圈按钮
- 单选框RadioGroup,单选按钮RadioButton的使用
- 单选框RadioGroup,单选按钮RadioButton的使用
- 安卓ListView中使用RadioGroup进行RadioButton的单项选择
- ImageView、RadioGroup、RadioButton、button
- RadioButton和RadioGroup的使用
- 底部按钮RadioButton、RadioGroup
- 安卓RadioButton的使用
- 安卓控件使用系列10:RadioButton单选按钮的使用
- 安卓程序中RadioGroup的使用
- Android PopupWindow RadioGroup RadioButton 的使用
- 控件RadioGroup、RadioButton和CheckBox的使用
- Android—RadioGroup和RadioButton的使用
- CRC算法 c语言 查表
- USACO 历年月赛 官方测试数据
- 仿写用户中心项目,主要运用管理系统和系统的用户
- Darwin架构优化的一点想法
- CDH 新加datanode的目录问题
- 安卓RadioButton,RadioGroup,Button按钮的使用
- KeyChain的封装
- vs如何创建和使用lib静态库
- view按椭圆轨迹移动
- Ubuntu vi 上下左右变ABCD问题解决方法
- [MySQL]触发器相关语句
- File类递归练习+JAVA学习笔记-DAY23
- MySQL学习第五篇:Xtrabackup的一些使用笔记
- Html5计时器实现电子表