RadioButton和CheckBox

1.在strings.xml中添加字符串

<resources>
    <string name="app_name">RadioCheck</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="male">男</string>
    <string name="famle">女</string>
<string name="red">红色</string>
<string name="black">黑色</string>
<string name="blue">蓝色</string>
</resources>

2.在布局文件中添加控件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">   
     <RadioButton
         android:id="@+id/male"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/male"/>
     <RadioButton
         android:id="@+id/famle"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/famle"/>
</RadioGroup>
<CheckBox
    android:id="@+id/red"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/red"/>
<CheckBox
    android:id="@+id/black"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/black"/>
<CheckBox
    android:id="@+id/blue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/blue"/>
    
</LinearLayout>


3完整代码

package com.example.radiocheck;


import android.app.Activity;
import android.os.Bundle;
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 radioGroup;
private RadioButton maleButton;
private RadioButton famleButton;

private CheckBox redBox ;
private CheckBox blackBox;
private CheckBox blueBox;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //通过控件的ID来得到代表控件的对象
        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
        maleButton = (RadioButton)findViewById(R.id.male);
        famleButton = (RadioButton)findViewById(R.id.famle);
        
        blueBox = (CheckBox)findViewById(R.id.blue);
        blackBox = (CheckBox)findViewById(R.id.black);
        redBox = (CheckBox)findViewById(R.id.red);
        
        
        //设置被选中的触发事件
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(maleButton.getId()==checkedId){
Toast.makeText(getApplicationContext(), "男",Toast.LENGTH_SHORT).show();
}else if(famleButton.getId()==checkedId)
{
Toast.makeText(getApplicationContext(), "女",Toast.LENGTH_SHORT).show();
}

}
});
        
        blueBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(), "蓝色", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(getApplicationContext(), "蓝色取消", Toast.LENGTH_SHORT).show();
}
}
});
        
        redBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(), "红色", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(getApplicationContext(), "红色取消", Toast.LENGTH_SHORT).show();
}
}
});


        blackBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         if(isChecked){
         Toast.makeText(getApplicationContext(), "黑色", Toast.LENGTH_SHORT).show();
         }else
         {
         Toast.makeText(getApplicationContext(), "黑色取消", Toast.LENGTH_SHORT).show();
         }
         }
        });
    }
}

4.显示效果



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值