个人爱好选择

通过对CheckBox多选项的应用,构建一个个人爱好选择的小程序。显示效果如图,可实现多选项的应用。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hades.homework4.MainActivity">

    <LinearLayout
        android:layout_width="372dp"
        android:layout_height="497dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="6dp"
        tools:layout_editor_absoluteY="7dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="41dp"
            android:text="你选择的是"
            android:textSize="20dp"
            tools:layout_editor_absoluteX="1dp"
            tools:layout_editor_absoluteY="96dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="8dp">

            <CheckBox
                android:id="@+id/checkBox1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="音乐"
                tools:layout_editor_absoluteX="8dp"
                tools:layout_editor_absoluteY="169dp" />

            <CheckBox
                android:id="@+id/checkBox2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="美术" />

            <CheckBox
                android:id="@+id/checkBox3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="课程" />

            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="确定" />

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="清除" />
        </LinearLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.example.hades.homework4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView tv;
    Button bt, cls;
    CheckBox cb1, cb2, cb3;
    String status = "你选择的是:";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView);
        bt = (Button) findViewById(R.id.button);
        bt.setOnClickListener(new click());
        cls = (Button) findViewById(R.id.button2);
        cls.setOnClickListener(new clear());
        cb1 = (CheckBox) findViewById(R.id.checkBox1);
        cb2 = (CheckBox) findViewById(R.id.checkBox2);
        cb3 = (CheckBox) findViewById(R.id.checkBox3);
    }

    private class click implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (cb1.isChecked()) {
                status += cb1.getText();
            }
            if (cb2.isChecked()) {
                status += cb2.getText();
            }
            if (cb3.isChecked()) {
                status += cb3.getText();
            }
            tv.setText(status);
        }
    }

    private class clear implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            status = "你选择的是:";
            cb1.setChecked(false);
            cb2.setChecked(false);
            cb3.setChecked(false);
            tv.setText(status);
        }
    }
}

这里写图片描述

这里写图片描述

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值