安卓实现注册并将信息显示在页面

一、页面布局


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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:id="@+id/user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/userText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:"
            android:textSize="25dp"/>

        <EditText
            android:id="@+id/username"
            android:layout_width="300dp"
            android:layout_height="40dp" />
    LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/passText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="25dp" />

        <EditText
            android:id="@+id/password"
            android:layout_width="300dp"
            android:layout_height="40dp" />
    LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性别:"
            android:textSize="25dp"/>

        <RadioGroup
            android:id="@+id/gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/man"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="25dp"
                android:text=""/>
            <RadioButton
                android:id="@+id/woman"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="25dp"
                android:text=""/>

        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:textSize="25dp"
            android:text="爱好:"/>

        <CheckBox
            android:id="@+id/basketball"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="篮球"
            android:textSize="25dp"/>
        <CheckBox
            android:id="@+id/badminton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="羽毛球"
            android:textSize="25dp"/>

    LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="75dp">
        <CheckBox
            android:id="@+id/football"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="足球"
            android:textSize="25dp"/>
        <CheckBox
            android:id="@+id/pingpong"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="乒乓球"
            android:textSize="25dp"/>
    LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center">
        <Button
            android:id="@+id/btn"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="确认"/>

        <TextView
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25dp"
            android:text="结果显示"
            android:textAlignment="center"/>
    LinearLayout>


LinearLayout>

二、Java代码

package com.lee.buttontest;

import androidx.appcompat.app.AppCompatActivity;

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

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText username;
    EditText password;
    RadioGroup radioGroup;
    RadioButton man;
    RadioButton woman;
    CheckBox basktball;
    CheckBox badminton;
    CheckBox pingpong;
    CheckBox football;
    Button btn;
    TextView result;
    String resultStr = "";
    List<CheckBox> hobby = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        radioGroup = findViewById(R.id.gender);
        man = findViewById(R.id.man);
        woman = findViewById(R.id.woman);
        basktball = findViewById(R.id.basketball);
        badminton = findViewById(R.id.badminton);
        pingpong = findViewById(R.id.pingpong);
        football = findViewById(R.id.football);
        btn = findViewById(R.id.btn);
        result = findViewById(R.id.result);
        hobby.add(basktball);
        hobby.add(badminton);
        hobby.add(pingpong);
        hobby.add(football);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String user = username.getText().toString();
        resultStr += "姓名:" + user + "\n";
        String pass = password.getText().toString();
        resultStr += "密码:" + pass + "\n";
        if(man.isChecked()){
            resultStr += "性别:" + man.getText().toString() + "\n";
        }
        if(woman.isChecked()){
            resultStr += "性别:" + woman.getText().toString() + "\n";
        }
        resultStr += "爱好:";
        for(CheckBox h : hobby){
            if(h.isChecked()){
                resultStr += h.getText().toString() + "、";
            }
        }
        result.setText(resultStr);
    }
}

三、结果展示

安卓实现注册并将信息显示在页面_第1张图片

你可能感兴趣的:(Android)