android 中button 的应用

本文介绍了一个简单的Android应用程序,该程序使用RadioButton和CheckBox实现用户性别选择和兴趣爱好的勾选功能,并详细展示了如何为这些控件设置监听器以响应用户的操作。

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

package com.example.testbutton;

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

public class MainActivity extends Activity {

	// 对控件对象进行定义
	private RadioGroup genderGroup = null;
	private RadioButton male = null;
	private RadioButton female = null;
	private CheckBox read = null;
	private CheckBox swim = null;
	private CheckBox run = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 通过对象ID 取得代表控件的对象
		genderGroup = (RadioGroup) findViewById(R.id.genderGroup);
		male = (RadioButton) findViewById(R.id.male);
		female = (RadioButton) findViewById(R.id.female);
		read = (CheckBox) findViewById(R.id.read);
		run = (CheckBox) findViewById(R.id.run);
		swim = (CheckBox) findViewById(R.id.swim);
		// 为RadioGroup设置监听器,注意这里的监听器和Button控件的监听器有所不同
		genderGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						if (female.getId() == checkedId) {
							System.out.println("female");

						} else if (male.getId() == checkedId) {
							System.out.println("male");

						}

					}
				});
		// 为多选按钮添加监听器
		read.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					System.out.print("read is checked");

				} else {
					System.out.print("read is unchecked");
				}

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

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					System.out.print("run is checked");

				} else {
					System.out.print("run is unchecked");
				}

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

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					System.out.print("swim is checked");

				} else {
					System.out.print("swim is unchecked");
				}

			}
		});

	}
}



页面layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <RadioGroup
        android:id="@+id/genderGroup"
        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:checked="true"
            android:text="@string/male" />

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

    </RadioGroup>

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

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

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

</LinearLayout>

strings.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">testbutton</string>
    <string name="action_settings">Settings</string>
    <string name="hello">Hello World, RadioTest!</string>
    <string name="male">male button</string>
    <string name="female">female button</string>
    <string name="swim">swim</string>
    <string name="run">run</string>
    <string name="read">read</string>
   

</resources>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值