一、实验目的
练习复选框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);
}
});
}
}