安卓学习(课后作业:用户注册)

案例演示:实现用户注册功能

在这里插入图片描述

步骤一:创建安卓应用(UserRegistered)

在这里插入图片描述

步骤二:基于模板创建注册窗口RegisteredActivity

在这里插入图片描述

  • 具体操作可以看

https://blog.csdn.net/X_Serendipity/article/details/125255114?spm=1001.2014.3001.5502

步骤三:在字符资源文件里定义变量

<resources>
    <string name="app_name">用户注册</string>
    <string name="username">姓名:</string>
    <string name="gender">性别:</string>
    <string name="age">年龄:</string>
    <string name="telephone">电话:</string>
    <string name="email">邮箱:</string>
    <string name="homepage">主页:</string>
    <string name="remark">备注:</string>
    <string name="enroll">注册</string>
    <string name="cancel">取消</string>
</resources>

步骤四:编写注册窗口布局资源文件(activity_registerde.xml)

<?xml version="1.0" encoding="utf-8"?>
<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:background="@drawable/bg"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".RegisteredActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/username"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/edt_username"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            android:singleLine="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/gender"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/edt_gender"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            android:singleLine="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/age"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/edt_age"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            android:singleLine="true" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_telephone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/telephone"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/edt_telephone"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            android:singleLine="true" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/email"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/edt_email"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            android:singleLine="true" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_homepage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/homepage"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/edt_homepage"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            android:singleLine="true" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_remark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/remark"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/edt_remark"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            android:singleLine="false" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_enroll"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:text="@string/enroll"
            android:onClick="doEroll"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="@string/cancel"
            android:onClick="doCancel"
            android:textSize="20sp" />

    </LinearLayout>

</LinearLayout>

步骤五:编写主窗口布局资源文件(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:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textColor="#0000ff"/>

</LinearLayout>

步骤六:编写项目清单文件AndroidManifest.xml

  • 删除MainActivity元素的意图过滤器
    在这里插入图片描述

步骤七:编写注册窗口(RegisteredActivity)

package com.liufanrong.userregistered;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class RegisteredActivity extends AppCompatActivity {
    private EditText edtUsername;
    private EditText edtGender;
    private EditText edtAge;
    private EditText edtTelephone;
    private EditText edtEmail;
    private EditText edtHomepage;
    private EditText edtRemark;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 利用布局资源设置用户界面
        setContentView(R.layout.activity_registered);
        // 通过资源标识符获取控件实例
        edtUsername = findViewById(R.id.edt_username);
        edtGender = findViewById(R.id.edt_gender);
        edtAge = findViewById(R.id.edt_age);
        edtTelephone = findViewById(R.id.edt_telephone);
        edtEmail = findViewById(R.id.edt_email);
        edtHomepage = findViewById(R.id.edt_homepage);
        edtRemark = findViewById(R.id.edt_remark);
    }
    /**
     * 注册事件处理方法
     *
     * @param v
     */
    public void doEroll(View v) {
        // 获取用户输入的数据
        String strUsername = edtUsername.getText().toString().trim();
        String strGender = edtGender.getText().toString().trim();
        String strAge = edtAge.getText().toString().trim();
        String strTelephone = edtTelephone.getText().toString().trim();
        String strEmail = edtEmail.getText().toString().trim();
        String strHomepage = edtHomepage.getText().toString().trim();
        String strRemark = edtRemark.getText().toString().trim();

        if (!"".equals(strUsername) && !"".equals(strGender) && !"".equals(strAge) && !"".equals(strTelephone) && !"".equals(strEmail)  && !"".equals(strHomepage) && !"".equals(strRemark)) {
            Toast.makeText(RegisteredActivity.this,"恭喜,注册成功!", Toast.LENGTH_SHORT).show();
            // 创建显示意图
            Intent intent = new Intent(RegisteredActivity.this,MainActivity.class);
            // 通过意图携带数据
            intent.putExtra("username",strUsername);
            intent.putExtra("gender",strGender);
            intent.putExtra("age",strAge);
            intent.putExtra("telephone",strTelephone);
            intent.putExtra("email",strEmail);
            intent.putExtra("homepage",strHomepage);
            intent.putExtra("remark",strRemark);
            // 按照意图启动目标组件
            startActivity(intent);
        }else {
            Toast.makeText(RegisteredActivity.this,"请将信息填写完整~", Toast.LENGTH_SHORT).show();
        }
    }
    /**
     * 取消事件处理方法
     *
     * @param v
     */
    public void doCancel(View v) {
        // 关闭当前窗口
        finish();
    }
}

步骤八:修改主界面MainActivity

package com.liufanrong.userregistered;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView tvMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 通过资源标识符获取控件实例
        tvMessage = findViewById(R.id.tvMessage);
        // 获得意图
        Intent intent = getIntent();
        // 判断意图是否为空
        if (intent != null) {
            // 获得意图携带数据
            String username = intent.getStringExtra("username");
            String gender = intent.getStringExtra("gender");
            String age = intent.getStringExtra("age");
            String telephone = intent.getStringExtra("telephone");
            String email = intent.getStringExtra("email");
            String homepage = intent.getStringExtra("homepage");
            String remark = intent.getStringExtra("remark");

            // 拼接用户信息
            String message = "注册成功\n 姓名:" + username + "\n性别:" + gender + "\n年龄:" + age + "\n电话:" + telephone + "\n邮箱:" + email + "\n主页:" + homepage + "\n简介:" + remark;
            // 设置标签属性
            tvMessage.setText(message);
        }
    }
}

测试结果

  • 注册成功的结果
    在这里插入图片描述
  • 注册失败的结果
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值