Android RadioButton的基本使用
一、 说明
本文主要讲一下Android 的RadioButton的基本知识。
二、 所用工具
Android Studio
三、 具体内容
1. RadioButton
单选按钮,需要放在RadioGroup中实现单选
2. 应用场景
选择性别
3. 例子
Xml代码:
<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择性别:"
android:textSize="23dp"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/btnMan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/>
<RadioButton
android:id="@+id/btnWoman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
</LinearLayout>
<Button
android:id="@+id/btnpost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="提交"/>
<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择性别:"
android:textSize="23dp"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/btnMan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/>
<RadioButton
android:id="@+id/btnWoman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
</LinearLayout>
<Button
android:id="@+id/btnpost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="提交"/>
</LinearLayout></LinearLayout>
效果图:
Activity代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取对应的组件
final RadioGroup rgSex = findViewById(R.id.radioGroup);
Button btnSubmit = findViewById(R.id.btn_Submit);
//设置监听事件
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取选中的值
for (int i = 0; i < rgSex.getChildCount(); i++) {
RadioButton rbSex = (RadioButton) rgSex.getChildAt(i);
if (rbSex.isChecked()) {
Toast.makeText(getApplicationContext(), "你选择的是:" + rbSex.getText(), Toast.LENGTH_LONG).show();
break;
}
}
}
});
}
}
点击提交后的效果图:
版权声明:本文为weixin_44547431原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。