Android实验 -中级控件(勾选框)

一、实验目的

        练习复选框CheckBox、开关按钮Switch、单选按钮RadioButton知识。

二、 实验内容

        勾选复选框后,点击提交按钮后,在屏幕下方显示勾选的内容,例如:屏幕显示“您喜欢吃梨子、苹果”。

三、 实验环境

        部署有Android Studio和Android SDK的主机

四、上机操作参考步骤

        点击梨子和苹果的勾选框,再点击提交按钮,文本框中显示“我喜欢吃的水果:梨子苹果”

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout    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="wrap_content"
    android:orientation="vertical"
    android:background="#FDFFFC"
    tools:context=".MainActivity"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/green"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_gravity="center"
            android:text="问卷调查"
            android:textSize="35dp"
            android:textStyle="bold" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="你喜欢吃什么水果?"
            android:textSize="25dp"
            android:textColor="@color/grey"
            android:layout_marginTop="15dp"
            />
    </LinearLayout>
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <CheckBox
            android:id="@+id/checkbox1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:text="梨子"
            android:textSize="45dp"

            >
        </CheckBox>
        <CheckBox
            android:id="@+id/checkbox2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:text="苹果"
            android:textSize="45dp"
            >
        </CheckBox>
        <CheckBox
            android:id="@+id/checkbox3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:text="橘子"
            android:textSize="45dp"
            >
        </CheckBox>
    </RadioGroup>
    <Button
        android:background="@drawable/choice"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="提交"
        android:textSize="30dp"
        android:textColor="@color/black"
        >
    </Button>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv1"
        android:textSize="30dp"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="140dp"
        android:background="@color/grey"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:gravity="center"
        android:text="欢迎进入问卷调查App"
        android:textSize="20dp"/>

</LinearLayout>

java文件

package com.example.myapplication6_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button1);
        CheckBox checkBox1=findViewById(R.id.checkbox1);
        CheckBox checkBox2=findViewById(R.id.checkbox2);
        CheckBox checkBox3=findViewById(R.id.checkbox3);
        TextView textView =findViewById(R.id.tv1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fruits=" ";
                if(checkBox1.isChecked())
                    fruits+=checkBox1.getText().toString();
                if(checkBox2.isChecked())
                    fruits+=checkBox2.getText().toString();
                if(checkBox3.isChecked())
                    fruits+=checkBox3.getText().toString();
                textView.setText("我喜欢吃的水果:"+fruits);
            }
        });
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值