基本用法
RadioButton:将 RadioButton 放到 <RadioGroup>按钮组中,从而实现单选功能。
Checkbox:用法比较简单,这里直接看例程。
RadioButton示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择性别"
android:textSize="30dp"/>
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"/>
</RadioGroup>
</LinearLayout>
Checkbox示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择士多里最钟爱的水果"
android:textSize="20dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="苹果"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="橙"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"/>
</LinearLayout>
效果图
获取选择的值
RadioButton
setOnCheckChangeListener 事件监听器
package com.example.test_ui;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
//声明空间
RadioGroup radio = findViewById(R.id.rg);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
//为RadioGroup设置一个监听器:setOnCheckedChangeListener()
radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radbutton = (RadioButton) findViewById(checkedId);
Toast.makeText(getApplicationContext(), "" + radbutton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
解析:为 RadioGroup 设置状态改变监听器,通过 checkedId 确认状态发生改变的按钮;再输出。
isChecked() 直接获取
package com.example.test_ui;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
private RadioButton man;
private RadioButton woman;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
man = findViewById(R.id.b1);
woman = findViewById(R.id.b2);
Button butt = findViewById(R.id.button_dataup);
//设置提交按钮点击事件
butt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(man.isChecked())
Toast.makeText(Main2Activity.this,"男",Toast.LENGTH_SHORT).show();
else if(woman.isChecked())
Toast.makeText(Main2Activity.this,"女",Toast.LENGTH_SHORT).show();
}
});
}
}
解析:这里我们为提交按键设置了点击监听事件
通过 isChecked( ) 判断按钮是否选中
与此类似的还有
getChildCount( ) 获得按钮组中的单选按钮的数目
getChinldAt() 根据索引值获取我们的单选按钮
效果图
图1为 RadioGroup 事件监听器 setOnCheckChangeListener
图2为RaadioButton 的 isChecked() 函数,获取单选按钮状态
Checkbox
.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择士多里最钟爱的水果"
android:textSize="20dp"/>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="苹果"/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="橙"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"/>
</LinearLayout>
.java文件
package com.example.test_ui;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
CheckBox cb_1 = findViewById(R.id.checkbox1);
CheckBox cb_2 = findViewById(R.id.checkbox2);
//按钮状态发生改变的回调函数
cb_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(compoundButton.isChecked())
Toast.makeText(getApplicationContext(),"苹果",Toast.LENGTH_SHORT).show();
}
});
cb_2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(getApplicationContext(),"橙",Toast.LENGTH_SHORT).show();
}
});
}
}
效果