[Android]实验2.1 用户界面

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 运行图
  • 代码

一、运行图 

运行效果:

 

二、代码

MainActivity.java  代码如下(示例): 

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

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 java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private EditText edit;
    private RadioGroup genderGroup;
    private RadioButton male,female;
    private String info="",sex="",major="",hobby="";
    private Button btn1,btn2;
    private Spinner spinner;
    private List<String> majorlist;
    private CheckBox hobby1,hobby2,hobby3,hobby4;
    private Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit=findViewById(R.id.edit);
        genderGroup=findViewById(R.id.genderGroup);
        male=findViewById(R.id.male);
        female=findViewById(R.id.female);
        spinner=findViewById(R.id.spinner);
        hobby1=findViewById(R.id.hobby1);
        hobby2=findViewById(R.id.hobby2);
        hobby3=findViewById(R.id.hobby3);
        hobby4=findViewById(R.id.hobby4);

        btn1=findViewById(R.id.btn1);
        btn2=findViewById(R.id.btn2);
        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton rd=findViewById(i);
                sex=rd.getText().toString();
            }
        });
        majorlist=new ArrayList<>();
        majorlist.add("计算机科学与技术");
        majorlist.add("人工智能");
        majorlist.add("软件工程");
        majorlist.add("信息安全");
        majorlist.add("大数据");
        majorlist.add("网络工程");
        ArrayAdapter<String> data=new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item,majorlist);
        data.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(data);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                major=majorlist.get(i);
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        CompoundButton.OnCheckedChangeListener Hobby=new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                hobby="";
                if(hobby1.isChecked())
                    hobby+=hobby1.getText()+",";
                if(hobby2.isChecked())
                    hobby+=hobby2.getText()+",";
                if(hobby3.isChecked())
                    hobby+=hobby3.getText()+",";
                if(hobby4.isChecked())
                    hobby+=hobby4.getText()+",";
            }
        };
        hobby1.setOnCheckedChangeListener(Hobby);
        hobby2.setOnCheckedChangeListener(Hobby);
        hobby3.setOnCheckedChangeListener(Hobby);
        hobby4.setOnCheckedChangeListener(Hobby);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(edit.getText().toString().equals(""))
                    toast.makeText(btn1.getContext(), "请输入您的姓名!",Toast.LENGTH_SHORT).show();
                else{
                    info="你好,"+edit.getText().toString()+"!\n";
                    info+="你的性别是:"+sex+"!\n";
                    info+="你的专业是"+major+"!\n";
                    info+="你的个人爱好有:"+hobby.substring(0,hobby.length()-1)+"!\n";
                    toast.makeText(btn1.getContext(),info,Toast.LENGTH_SHORT).show();
                }
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }
}

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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/name"
            android:text="姓名"
            android:textSize="22sp"
            android:textStyle="bold"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit"
            android:textSize="18sp"
            android:ems="10"
            />
    </LinearLayout>
    <!--    单选按钮-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:id="@+id/sex"
            android:text="性别"
            android:textSize="22sp"
            android:textStyle="bold"
            />
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/genderGroup"
            android:orientation="horizontal"
            >
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/male"
                android:text="男"
                android:textSize="18sp" />
            <!--            android:checked="true"-->

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/female"
                android:text="女"
                android:textSize="18sp"
                />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/major"
            android:text="专业"
            android:textSize="22sp"
            android:textStyle="bold"/>
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinner"
            />

        <!--        CheckBox按钮-->
    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hobby"
        android:text="兴趣爱好"
        android:textSize="22sp"
        android:textStyle="bold"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/hobby1"
            android:text="@string/hobby1"
            android:textSize="18sp"
            />
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/hobby2"
            android:text="@string/hobby2"
            android:textSize="18sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/hobby3"
            android:text="@string/hobby3"
            android:textSize="18sp"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/hobby4"
            android:text="@string/hobby4"
            android:textSize="18sp"
            />
    </LinearLayout>


    <!--    button按钮-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8sp"
            android:layout_marginRight="20sp"
            android:id="@+id/btn1"
            android:text="提交"
            android:textSize="30sp"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8sp"
            android:id="@+id/btn2"
            android:text="取消"
            android:textSize="30sp"/>
    </LinearLayout>

</LinearLayout>

string.xml 代码如下(示例):

<resources>
    <string name="app_name">My Application</string>
    <string name="hobby1">音乐</string>
    <string name="hobby2">运动</string>
    <string name="hobby3">游泳</string>
    <string name="hobby4">阅读</string>
</resources>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值