RadioButton、CheckBox和Toast的使用

package com.duoguo.android;

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

/**
 * 1、CheckBox的使用; 2、RadioButton的使用; 3、Toast的使用。
 *
 * @author shyboy(897948924@qq.com)
 *
 */
public class ButtonActivityActivity extends Activity {

 private RadioGroup genderRadioGroup;
 private RadioButton maleRadioButton;
 private RadioButton femaleRadioButton;

 private CheckBox footballCheckBox;
 private CheckBox basketballCheckBox;
 private CheckBox volleyballCheckBox;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  genderRadioGroup = (RadioGroup) findViewById(R.id.genderRadioGroupId);
  maleRadioButton = (RadioButton) findViewById(R.id.maleRadioButtonId);
  femaleRadioButton = (RadioButton) findViewById(R.id.femaleRadioButtonId);

  footballCheckBox = (CheckBox) findViewById(R.id.footballCheckBoxId);
  basketballCheckBox = (CheckBox) findViewById(R.id.basketballCheckBoxId);
  volleyballCheckBox = (CheckBox) findViewById(R.id.volleyballCheckBoxId);

  // 为RadioGroup添加单击监听事件
  genderRadioGroup
    .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

     @Override
     public void onCheckedChanged(RadioGroup group, int checkedId) {

      if (maleRadioButton.getId() == checkedId)// 当单选按钮被选中时
      {
       System.out.println("male");
       Toast.makeText(ButtonActivityActivity.this,
         "您选择的是" + maleRadioButton.getText(),
         Toast.LENGTH_LONG).show();
      } else if (femaleRadioButton.getId() == checkedId) {
       System.out.println("female");
       Toast.makeText(ButtonActivityActivity.this,
         "您选择的是:" + femaleRadioButton.getText(),
         Toast.LENGTH_LONG).show();
      }

     }
    });

  // 为CheckBox添加单击监听事件
  footballCheckBox
    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

     @Override
     public void onCheckedChanged(CompoundButton buttonView,
       boolean isChecked) {

      if (isChecked) {
       System.out.println("football");
       Toast.makeText(ButtonActivityActivity.this,
         "原来您的爱好是:" + footballCheckBox.getText(),
         Toast.LENGTH_LONG).show();
      } else {
       Toast.makeText(ButtonActivityActivity.this,
         "原来您不喜欢的是:" + footballCheckBox.getText(),
         Toast.LENGTH_LONG).show();
      }

     }
    });

  basketballCheckBox
    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

     @Override
     public void onCheckedChanged(CompoundButton buttonView,
       boolean isChecked) {

      if (isChecked) {
       System.out.println("basketball");
       Toast.makeText(ButtonActivityActivity.this,
         "原来您的爱好是:" + basketballCheckBox.getText(),
         Toast.LENGTH_LONG).show();
      } else {
       Toast.makeText(ButtonActivityActivity.this,
         "原来您不喜欢的是:" + basketballCheckBox.getText(),
         Toast.LENGTH_LONG).show();
      }

     }
    });

  volleyballCheckBox
    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

     @Override
     public void onCheckedChanged(CompoundButton buttonView,
       boolean isChecked) {

      if (isChecked) {
       System.out.println("volleyball");
       Toast.makeText(ButtonActivityActivity.this,
         "原来您的爱好是:" + volleyballCheckBox.getText(),
         Toast.LENGTH_LONG).show();
      } else {
       Toast.makeText(ButtonActivityActivity.this,
         "原来您不喜欢的是:" + volleyballCheckBox.getText(),
         Toast.LENGTH_LONG).show();
      }

     }
    });

 }
}

 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">

 <RadioGroup android:id="@+id/genderRadioGroupId"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:orientation="vertical">

  <RadioButton android:id="@+id/maleRadioButtonId"
   android:layout_width="wrap_content" android:layout_height="wrap_content"
   android:text="@string/male" />

  <RadioButton android:id="@+id/femaleRadioButtonId"
   android:layout_width="wrap_content" android:layout_height="wrap_content"
   android:text="@string/female" />

 </RadioGroup>

 <CheckBox android:id="@+id/footballCheckBoxId"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:text="@string/football" />

 <CheckBox android:id="@+id/basketballCheckBoxId"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:text="@string/basketball" />

 <CheckBox android:id="@+id/volleyballCheckBoxId"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:text="@string/volleyball" />

</LinearLayout>

 

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="app_name">ButtonActivity</string>
 <string name="male">男</string>
 <string name="female">女</string>
 <string name="football">足球</string>
 <string name="basketball">篮球</string>
 <string name="volleyball">排球</string>
</resources>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值