android控件05---RadioButton(RadioGroup)
来源:互联网 发布:魔法王麒麟臂升阶数据 编辑:程序博客网 时间:2024/05/21 10:36
RadioButton表示一个单选按钮,它有选中和不选中2种状态。
RadioGroup组件被称为单选按钮组,因为它可以有很多个单选按钮,并且规定在这个组里面只能有一个单选按钮是选中状态。也就是说,但选择其中一个单选按钮时,会取消这个组中其他已经被选择的按钮的选中状态。
依旧附上一个小例子:
Activity里的代码如下:
RadioGroup组件被称为单选按钮组,因为它可以有很多个单选按钮,并且规定在这个组里面只能有一个单选按钮是选中状态。也就是说,但选择其中一个单选按钮时,会取消这个组中其他已经被选择的按钮的选中状态。
依旧附上一个小例子:
XML布局中的代码如下(采用的是RelativeLayout布局):
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:text="下列哪个选项不属于android控件?" android:textSize="20dp" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_below="@+id/textView" android:layout_alignParentStart="true"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textView" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="EditText" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <RadioButton android:id="@+id/radioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android" /> </RadioGroup></RelativeLayout>
Activity里的代码如下:
public class MainActivity extends AppCompatActivity { private TextView textView; private RadioGroup radioGroup; private RadioButton radio1; private RadioButton radio2; private RadioButton radio3; private RadioButton radio4; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView); radioGroup =(RadioGroup)findViewById(R.id.radioGroup); radio1 = (RadioButton)findViewById(R.id.radioButton1); radio2 = (RadioButton)findViewById(R.id.radioButton2); radio3 = (RadioButton)findViewById(R.id.radioButton3); radio4 = (RadioButton)findViewById(R.id.radioButton4); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group,int checkedId) { if (checkedId == radio4.getId()){ DisplayToast("回答正确!"); }else{ DisplayToast("回答错误!"); } } }); } public void DisplayToast(String str){ //创建Toast对象,设置Toast显示的位置,显示Toast Toast toast = Toast.makeText(MainActivity.this, str , Toast.LENGTH_LONG); toast.setGravity(Gravity.TOP,0,500); toast.show(); }}
效果图如下:
阅读全文
1 0
- android控件05---RadioButton(RadioGroup)
- Android控件RadioButton&RadioGroup
- Android-基本控件(CheckBox,RadioGroup,RadioButton)
- android 控件之RadioGroup&RadioButton
- Android控件之RadioGroup、RadioButton
- Android控件RadioButton&RadioGroup&CheckBox
- Android常用控件(一)【RadioGroup、RadioButton、CheckBox、Toast】
- Android控件系列之RadioButton&RadioGroup
- Android UI控件之RadioGroup、RadioButton
- android 控件 单项选择(RadioGroup,RadioButton)
- Android控件系列之RadioButton&RadioGroup
- Android控件系列之RadioButton&RadioGroup
- Android控件系列之RadioButton&RadioGroup
- Android控件系列之RadioButton&RadioGroup
- Android控件系列之RadioButton&RadioGroup
- Android控件——RadioButton和RadioGroup
- Android基本控件RadioGroup和RadioButton学习
- Android按钮控件之RadioGroup和RadioButton
- centos7 mysql数据库安装和配置
- 我的Java第二课下
- QSettings操作配置文件
- JFinal连接数据库失败
- 微信开发–带参数的二维码
- android控件05---RadioButton(RadioGroup)
- mac上java,android home的配置
- Python-json与pickle数据序列化
- Java的package
- 《effective c++》笔记一
- HDU 1885 Key Task 状态压缩+BFS
- 【Nginx】在Centos 7 系统下安装Nginx
- Redis使用详细教程
- 简单学生选课系统之多种方式查询