android注册页面小练习

  • activity_homework.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/black">
        <ImageView
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:src="@mipmap/ic_launcher_round"
            android:layout_gravity="center"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="填写用户信息"
            android:textSize="25sp"
            android:textColor="@color/white"
            android:padding="15dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginTop="25dp"
        android:layout_alignParentBottom="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_weight="4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="用户名:"
                android:textSize="25sp"
                android:textColor="@color/black"/>
            <EditText
                android:id="@+id/username"
                android:layout_weight="6"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:hint="username"
                android:textSize="25sp"
                android:outlineAmbientShadowColor="@color/colorPrimaryDark"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_weight="4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="密    码:"
                android:textSize="25sp"
                android:textColor="@color/black"/>
            <EditText
                android:id="@+id/password"
                android:layout_weight="6"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:hint="password"
                android:textSize="25sp"
                android:outlineAmbientShadowColor="@color/colorPrimaryDark"/>
        </LinearLayout>
        <RadioGroup
            android:id="@+id/rg_sex"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginTop="12dp">
                <TextView
                    android:layout_weight="4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="性    别:"
                    android:textSize="25sp"
                    android:textColor="@color/black"/>
                <RadioButton
                    android:id="@+id/rb_man"
                    android:layout_weight="3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textSize="25sp"
                    android:checked="true"
                    android:onClick ="onRadioButtonClicked"/>
                <RadioButton
                    android:id="@+id/rb_woman"
                    android:layout_weight="3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textSize="25sp"
                    android:onClick ="onRadioButtonClicked"/>
            </LinearLayout>
            <Button
                android:id="@+id/btn_submit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="确定"
                android:textSize="25sp"
                android:background="@color/darkgrey"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="25dp"
                />
        </RadioGroup>
    </LinearLayout>
</RelativeLayout>
  • HomeWorkActivity
package com.example.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class HomeWorkActivity extends Activity {

    private Button btnSubmit;
    private EditText username;
    private EditText password;
    private RadioGroup rgroupSex;
    private RadioButton rbWoman;
    private RadioButton rbMan;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homework);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        rbMan = findViewById(R.id.rb_man);
        rbWoman = findViewById(R.id.rb_woman);
        btnSubmit = findViewById(R.id.btn_submit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = "";
                String unStr = username.getText().toString();
                String pwdStr = password.getText().toString();
                String sex = "";
                if (rbMan.isChecked()) {
                    sex = rbMan.getText().toString();
                } else {
                    sex = rbWoman.getText().toString();
                }
                str = unStr + " " + pwdStr + " " + sex;
                Toast.makeText(HomeWorkActivity.this, str, Toast.LENGTH_LONG).show();
            }
        });
    }

    public void onRadioButtonClicked(View view) {
        boolean checked = ((RadioButton) view).isChecked();
        switch (view.getId()) {
            case R.id.rb_man:
                if (checked) {
                    rbWoman = findViewById(R.id.rb_woman);
                    rbWoman.setChecked(false);
                    break;
                }
            case R.id.rb_woman:
                if (checked) {
                    rbMan = findViewById(R.id.rb_man);
                    rbMan.setChecked(false);
                    break;
                }
        }
    }
}

  • 运行截图

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mr.Letian

您的打赏是我最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值