Android常用组件 19软件工程

布局文件

<?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">

    <EditText
        android:id="@+id/edt_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入姓名" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rbt_boy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

    </RadioGroup>

    <Spinner
        android:id="@+id/sp_class"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/classes" />


    <CheckBox
        android:id="@+id/chk_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写代码" />

    <CheckBox
        android:id="@+id/chk_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跑步" />

    <CheckBox
        android:id="@+id/chk_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="游泳" />

    <CheckBox
        android:id="@+id/chk_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="追剧" />

    <Button
        android:id="@+id/btn_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确认" />


    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#33ff0000"
        android:text="Hello World!"
        android:textSize="20sp" />

</LinearLayout>

MainActivity文件

package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btn_ok;
    private TextView tv_show;
    private EditText edt_name;
    private RadioButton rbt_boy;
    private Spinner sp_class;
    private String class_name;
    private CheckBox chk_1;
    private CheckBox chk_2;
    private CheckBox chk_3;
    private CheckBox chk_4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_ok = findViewById(R.id.btn_ok);
        tv_show = findViewById(R.id.tv_show);
        edt_name = findViewById(R.id.edt_name);
        rbt_boy = findViewById(R.id.rbt_boy);
        sp_class = findViewById(R.id.sp_class);
        chk_1 = findViewById(R.id.chk_1);
        chk_2 = findViewById(R.id.chk_2);
        chk_3 = findViewById(R.id.chk_3);
        chk_4 = findViewById(R.id.chk_4);

        sp_class.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // 输入日志
                Log.e("MainActivity", "position = " + position);

                // 获取strings.xml中定义的数组内容
                String[] classArray = getResources().getStringArray(R.array.classes);
                class_name = classArray[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        // 快捷键 new 空格  CTRL+SHIFT+空格
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 让窗口弹出一个提示 Toast 吐司
                // 注释  CTRL + /
//                Toast.makeText(MainActivity.this,
//                        "您点击了按钮", Toast.LENGTH_SHORT).show();

                // 使用资源文件
                Toast.makeText(MainActivity.this,
                        R.string.app_name, Toast.LENGTH_SHORT).show();

                // 获取输入框的内容
                String name = edt_name.getText().toString();
                boolean isBoy = rbt_boy.isChecked();

                // 判断多选框有没有选中
                StringBuffer buffer = new StringBuffer();
                if(chk_1.isChecked()){
                    buffer.append(chk_1.getText().toString()).append(",");
                }
                if(chk_2.isChecked()){
                    buffer.append(chk_2.getText().toString()).append(",");
                }
                if(chk_3.isChecked()){
                    buffer.append(chk_3.getText().toString()).append(",");
                }
                if(chk_4.isChecked()){
                    buffer.append(chk_4.getText().toString());
                }

                if(isBoy){
                    tv_show.setText("你好帅哥," +class_name + ", " +  name + ",喜欢" + buffer.toString());
                }else{
                    tv_show.setText("你好美女," +class_name + ", " +  name+ ",喜欢" + buffer.toString());
                }
            }
        });
    }
}

strings.xml文件

<resources>
    <string name="app_name">Hello World</string>

    <array name="classes">
        <item>19软件工程1班</item>
        <item>19软件工程2班</item>
        <item>18软件工程1班</item>
        <item>18软件工程2班</item>
    </array>
</resources>

在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值