[Android]实验一:基本控件和事件的使用

涉及知识点: Toast、CheckBox、Radio、Spinner和相关的点击事件

效果图

当没有输入姓名的时候会提示输入姓名
在这里插入图片描述

代码

MainActivity.java

package cn.edu.henu.homework;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import javax.security.auth.Subject;

public class MainActivity extends AppCompatActivity implements CheckBox.OnCheckedChangeListener{
    private Spinner spin;
    private EditText name;
    private RadioGroup gender;
    private CheckBox hobby1,hobby2,hobby3,hobby4;
    private Button submit,cancel;
    RadioButton male,female;
    private String result;
    private String hobbys="";
    private String genderRes="";
    private String majorRes="";
    private static final String[] majors={"计算机科学与技术","信息安全","软件工程","大数据","网络工程"};
    private ArrayAdapter<String> adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.spin=findViewById(R.id.spin);
        this.name=findViewById(R.id.name);
        this.male=findViewById(R.id.male);
        this.female=findViewById(R.id.female);
        this.hobby1=findViewById(R.id.hobby1);
        this.hobby2=findViewById(R.id.hobby2);
        this.hobby3=findViewById(R.id.hobby3);
        this.hobby4=findViewById(R.id.hobby4);
        this.submit=findViewById(R.id.submit);
        this.cancel=findViewById(R.id.cancel);
        this.gender=findViewById(R.id.gender);

        hobby1.setOnCheckedChangeListener(this);
        hobby2.setOnCheckedChangeListener(this);
        hobby3.setOnCheckedChangeListener(this);
        hobby4.setOnCheckedChangeListener(this);

        this.spin.setPrompt("请选择你的专业:");
        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,majors);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(adapter);
        spin.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                majorRes=majors[i];
                adapterView.setVisibility(View.VISIBLE);
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(i==male.getId()){
                    genderRes="男";
                }
                else {
                    genderRes = "女";
                }
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(name.getText().toString().equals("")){
                    Toast.makeText(submit.getContext(),"请输入您的姓名",Toast.LENGTH_LONG).show();
                }
                else{


                    result="";
                    result+="你好"+name.getText().toString()+"!"+"\n";
                    result+="你的性别是"+genderRes+"\n";
                    result+="你的专业是"+majorRes+"\n";
                    result+="你的个人爱好有"+hobbys+"\n";

                    Toast.makeText(submit.getContext(),result,Toast.LENGTH_LONG).show();
                }
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


            }
        });

    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        hobbys="";
        if(hobby1.isChecked())
            hobbys+=hobby1.getText().toString()+",";
        if(hobby2.isChecked())
            hobbys+=hobby2.getText().toString()+",";
        if(hobby3.isChecked())
            hobbys+=hobby3.getText().toString()+",";
        if(hobby4.isChecked())
            hobbys+=hobby4.getText().toString()+",";

        if(!hobbys.equals("")){
            hobbys=hobbys.substring(0,hobbys.length()-1);
        }
    }
}

activity_main.xml

<?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="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名"
            android:textSize="20sp"
            />

        <EditText
            android:id="@+id/name"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="8dp"
            android:text="性别"
            android:textSize="20sp"
            />

        <RadioGroup
            android:paddingBottom="10dp"
            android:id="@+id/gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <RadioButton
                android:id="@+id/male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:text=""/>
            <RadioButton
                android:id="@+id/female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:text=""/>
        </RadioGroup>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="专业"
            android:textSize="20sp"
            />
        <Spinner
            android:id="@+id/spin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:spinnerMode="dropdown">
        </Spinner>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="兴趣爱好"
            android:textSize="20sp"
            />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <CheckBox
                android:id="@+id/hobby1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="音乐"/>
            <CheckBox
                android:id="@+id/hobby2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="运动"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <CheckBox
                android:id="@+id/hobby3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="游泳"/>
            <CheckBox
                android:id="@+id/hobby4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="阅读"/>
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:id="@+id/submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="提交"/>
        <Button
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="取消"/>
    </LinearLayout>
</LinearLayout>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开心星人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值