`
shenjichao2009
  • 浏览: 95703 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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>

分享到:
评论

相关推荐

    android组件 RadioButton,CheckBox,Toast具体实例

    在Android开发中,RadioButton、...通过上述方法,我们可以灵活地使用RadioButton、CheckBox和Toast组件来增强Android应用的交互性和用户体验。在实际项目中,可以根据需求进行组合和扩展,创造出更多元化的功能。

    Radiogroup、Checkbox、Toast

    标题中的“Radiogroup、Checkbox、Toast”是Android开发中常用的三个组件,它们在构建用户界面时起着关键作用。让我们深入探讨这三个组件及其在Android应用开发中的使用。 Radiogroup 是一个布局容器,用于管理一...

    android RadioButton和CheckBox组件的使用方法

    本次实验中主要是学习如何使用RadioGroup,CheckBox,RadioButton和Toast这几个控件,android UI开发中也会经常用到他们

    CheckBox简单样例.7z

    在Android开发中,CheckBox是一种非常常见的用户界面组件,它允许...同时,也可以根据实际需求,将CheckBox与其他控件结合使用,比如与RadioButton一起构建多选一的选项组,或者与其他UI元素配合,实现更复杂的逻辑。

    Android设置CheckBox

    此外,如果需要对多个CheckBox进行联动或数据绑定,可以使用RadioGroup配合RadioButton,或者使用Material Design的SwitchCompat控件。在处理大量复选项时,还可以考虑使用CheckBox的子类,比如MultiChoiceAdapter,...

    应用源码之(CheckBox与监听).zip

    此外,CheckBox还常常与其他组件配合使用,如在Dialog、PopupWindow中,或者结合RadioButton实现互斥选择。开发者还需要注意线程同步问题,确保在UI线程中修改CheckBox的状态。 总之,本资料"应用源码之(CheckBox与...

    Android控件系列之CheckBox使用介绍

    了解了CheckBox的基本使用后,你还可以扩展到其他相关的Android控件,如RadioButton,它们在单选场景下十分有用。此外,对于复杂的用户交互,可能需要使用到自定义的CheckBox组件,这涉及到Android的自定义控件开发...

    安卓--OnCheckedChangeListener实例

    `OnCheckedChangeListener` 是 `CompoundButton` 类(`CheckBox` 和 `RadioButton` 的基类)的一个接口,用于监听`CheckBox`或`RadioButton`的选中状态改变。当用户切换选择状态时,会调用该接口的`onCheckedChanged...

    Android用户界面程序设计示例

    该示例涵盖了Android用户界面设计的多个方面,包括按钮、Toast弹出对话框、TextView文本框、EditText编辑框、RadioButton单选、CheckBox多选、Menu菜单、Dialog对话框等。 按钮和Toast弹出对话框 按钮是一种常见的...

    android用户界面程序设计实例

    例如,在一个登录界面中,我们可以使用按钮来触发登录操作,并使用 Toast 弹出对话框来显示登录结果。 二、TextView 文本框 TextView 文本框是 Android 界面中最常用的控件之一,用于显示文本信息。TextView ...

    android studio实现简单考试应用程序实例代码详解

    在考试应用程序中,我们可以使用Spinner、CheckBox、RadioButton 和 EditText 等控件来设计四种题型。这些控件可以帮助我们快速地设计和实现考试应用程序的各种题型。 knowledge point 5: setText 和 Toast 的使用 ...

    Android用户界面程序设计示例[收集].pdf

    可以使用 Toast 类来创建 Toast 弹出对话框,并使用 show 方法来显示它。 2. TextView 文本框 TextView 是 Android 中的一种常见的用户界面元素,用于显示文本。TextView 可以用来显示静态文本,也可以用来显示...

    AndriodSituationRadio-CheckBox

    本项目“AndriodSituationRadio-CheckBox”旨在深入探讨这两种控件的使用和事件处理,特别是如何在Java语言环境下进行操作。 `RadioGroup`是Android中的一个布局容器,它专门用来管理一系列`RadioButton`对象。`...

    Android 所有的控件使用代码

    Android还提供了许多其他控件,如`RadioButton`(单选按钮)和`CheckBox`(复选框)。创建它们并设置初始状态: ```java RadioButton radioButton = new RadioButton(this); radioButton.setText("选项1"); ...

    Android_cn_android.widget

    7. CheckBoxGroup 和 RadioButtonGroup: 这些不是标准的Android组件,但通常由开发者自定义实现,用于管理多个Checkbox或RadioButton的逻辑,确保一次只有一个RadioButton被选中或处理多个Checkbox的勾选状态。...

    android 常用基本控件

    RadioGroup和RadioButton结合可以实现单选功能。使用setOnCheckedChangeListener监听按钮状态变化,通过isChecked和getId判断选中项。 示例:创建一个选择题,用户选择答案后,根据选中项给出相应反馈,如"回答...

    Android笔记

    总的来说,这些笔记涵盖了Android中的Intent使用、UI元素(如EditText和Menu)的操作、布局管理(尤其是RelativeLayout的属性)以及基本的交互组件(如RadioGroup和CheckBox)的使用。这些都是Android开发中常见的...

Global site tag (gtag.js) - Google Analytics