android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发

本文介绍Android开发中单选按钮Radio与多选按钮CheckBox的应用。通过MainActivity代码示例展示了如何设置监听器并响应用户选择变化,同时给出了布局文件main.xml以实现界面元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发

本博文主要讲述的是android开发中的单选和多选按钮的使用,具体情况请看实例代码:

MainActivity.java:

package com.example.radiotest;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;


public class MainActivity extends Activity {


private RadioGroup generGroup = null;
private RadioButton maleButton = null;
private RadioButton femaleButton =null;

private CheckBox swin = null, run = null, jump = null;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

generGroup = (RadioGroup)findViewById(R.id.mySexGroup);
maleButton = (RadioButton)findViewById(R.id.maleButton);
femaleButton = (RadioButton)findViewById(R.id.femaleButton);

swin = (CheckBox)findViewById(R.id.swinBox);
run = (CheckBox)findViewById(R.id.runBox);
jump = (CheckBox)findViewById(R.id.jumpBox);


//设置RadioGroup监听器
generGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == maleButton.getId()){
Toast.makeText(MainActivity.this, "male", Toast.LENGTH_SHORT).show();
}
else if(checkedId == femaleButton.getId()){
Toast.makeText(MainActivity.this, "female", Toast.LENGTH_SHORT).show();
}
}
});



//设置CheckBox多选的监听器
swin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("swin is checked!");
}
else{
System.out.println("swin is unchecked!");
}
}
});

run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("run is checked!");
}
else{
System.out.println("run is unchecked!");
}
}
});


jump.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("jump is checked!");
}
else{
System.out.println("jump is unchecked!");
}
}
});


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}

布局文件main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <RadioGroup 
        android:id="@+id/mySexGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        
        <RadioButton 
            android:id="@+id/maleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male"/>
        
          <RadioButton 
            android:id="@+id/femaleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female"/>
          
    </RadioGroup>


    <CheckBox 
        android:id="@+id/swinBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/swin"/>
    
    <CheckBox 
        android:id="@+id/runBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/run"/>
    
    <CheckBox 
        android:id="@+id/jumpBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/jump"/>
    
    
</LinearLayout
>


效果如下:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值