个人作业登录+注册

package com.example.demo3;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.demo3.dao.StudentDao;
import com.example.demo3.entity.Student;

public class SignUpActivity extends AppCompatActivity {

    private EditText id;
    private EditText name;
    private EditText phone;
    private EditText className;
    private EditText password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        id = findViewById(R.id.StudentIdEdit);
        name = findViewById(R.id.NameEdit);
        phone = findViewById(R.id.PhoneEdit);
        className = findViewById(R.id.ClassEdit);
        password = findViewById(R.id.PasswordEdit);


        Button backLoginButton = findViewById(R.id.BackLoginButton);
        backLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回到 MainActivity
                Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }

    public void register(View view){

        String id1 = id.getText().toString();
        String name1 = name.getText().toString();
        String phone1 = phone.getText().toString();
        String className1 = className.getText().toString();
        String password1 = password.getText().toString();

        Student student = new Student();

        student.setId(id1);
        student.setName(name1);
        student.setPhone(phone1);
        student.setClassName(className1);
        student.setPassword(password1);
        student.setSetGoal(0);
        student.setSetRecord(0);

        new Thread(){
            @Override
            public void run() {

                int msg = 0;
                StudentDao studentDao = new StudentDao();

                Student s = studentDao.findStudent(student.getId());
                if(s != null){
                    msg = 1;
                }
                else{
                    boolean flag = studentDao.register(student);
                    if(flag){
                        msg = 2;
                    }
                }
                hand.sendEmptyMessage(msg);
            }
        }.start();
    }
    @SuppressLint("HandlerLeak")
    final Handler hand = new Handler()
    {
        public void handleMessage(Message msg) {
            if(msg.what == 0) {
                Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_LONG).show();
            } else if(msg.what == 1) {
                Toast.makeText(getApplicationContext(),"该账号已经存在,请换一个账号",Toast.LENGTH_LONG).show();
            } else if(msg.what == 2) {
                Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show();
                Intent intent = new Intent();
                //将想要传递的数据用putExtra封装在intent中
                intent.putExtra("a","注册");
                setResult(RESULT_CANCELED,intent);
                finish();
            }
        }
    };
}
package com.example.demo3;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.demo3.dao.StudentDao;
import com.example.demo3.entity.Student;

public class MainActivity extends AppCompatActivity {

    private Button signUpButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        signUpButton = findViewById(R.id.SignUpButton);

        signUpButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建一个意图对象,用于启动注册页面的活动
                Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
                // 启动注册页面的活动
                startActivity(intent);
            }
        });
    }


    /**
     * function: 登录
     * */
    public void login(View view){

        EditText EditTextAccount = findViewById(R.id.UserNameEdit);
        EditText EditTextPassword = findViewById(R.id.PassWordEdit);
        String account = EditTextAccount.getText().toString();
        String password = EditTextPassword.getText().toString();


        // 检查是否为教师账号,如果是则直接跳转到教师界面
        if (account.equals("teacher") && password.equals("teacher")) {
            Intent intent = new Intent(MainActivity.this, teacher.class);
            startActivity(intent);
        } else {
            new Thread() {
                @Override
                public void run() {
                    StudentDao studentDao = new StudentDao();
                    int msg = studentDao.login(EditTextAccount.getText().toString(), EditTextPassword.getText().toString());
                    hand1.sendEmptyMessage(msg);
                }
            }.start();
        }

    }

    @SuppressLint("HandlerLeak")
    final Handler hand1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
                // 登录成功后启动新的活动
                startActivity(new Intent(MainActivity.this, MainActivity2.class));
            } else if (msg.what == 0) {
                Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show();
            } else if (msg.what == 2) {
                Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show();
            } else if (msg.what == 3) {
                Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_LONG).show();
            }
        }
    };

}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".SignUpActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#F5F5F5">

        <!-- Logo -->
        <ImageView
            android:id="@+id/LogoImage"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:layout_marginTop="2dp"
            android:src="@drawable/gb1"/>

        <!-- 标题 -->
        <TextView
            android:id="@+id/TitleText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:text="Timing•注册"
            android:gravity="center"
            android:textStyle="italic"
            android:textColor="#808080"
            android:textSize="30dp" />

        <!-- 学号输入 -->
        <EditText
            android:id="@+id/StudentIdEdit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/translucent_edit"
            android:hint="输入学号"
            android:textSize="24dp"
            android:singleLine="true" />

        <!-- 姓名输入 -->
        <EditText
            android:id="@+id/NameEdit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/translucent_edit"
            android:hint="输入姓名"
            android:textSize="24dp"
            android:singleLine="true" />

        <!-- 手机号输入 -->
        <EditText
            android:id="@+id/PhoneEdit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/translucent_edit"
            android:hint="输入手机号"
            android:textSize="24dp"
            android:singleLine="true" />

        <!-- 班级输入 -->
        <EditText
            android:id="@+id/ClassEdit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/translucent_edit"
            android:hint="输入班级"
            android:textSize="24dp"
            android:singleLine="true" />

        <!-- 密码输入 -->
        <EditText
            android:id="@+id/PasswordEdit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/translucent_edit"
            android:hint="输入密码"
            android:textSize="24dp"
            android:maxLength="16"
            android:singleLine="true"
            android:inputType="textPassword" />


        <!-- 按钮布局 -->
        <LinearLayout
            android:id="@+id/ButtonLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <!-- 立即注册按钮 -->
            <Button
                android:id="@+id/SignUpButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="15dp"
                android:layout_margin="15dp"
                android:background="@drawable/translucent_button"
                android:text="立即注册"
                android:textColor="@color/dark"
                android:textSize="24dp"
                android:onClick="register"/>

            <!-- 返回登录按钮 -->
            <Button
                android:id="@+id/BackLoginButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="15dp"
                android:layout_margin="15dp"
                android:textColor="@color/dark"
                android:background="@drawable/translucent_button"
                android:text="返回登录"
                android:textSize="24dp" />

        </LinearLayout>

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <!--使用线性布局-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#F5F5F5">

        <!--Logo-->
        <ImageView
            android:id="@+id/LogoImage"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:layout_marginTop="100dp"
            android:src="@drawable/gb1"/>

        <!--标题-->
        <TextView
            android:id="@+id/TitleText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="25dp"
            android:text="Timing•登录"
            android:gravity="center"
            android:textStyle="italic"
            android:textColor="#808080"
            android:textSize="30dp" />

        <!--嵌套线性布局-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!--嵌套线性布局-->
            <LinearLayout
                android:id="@+id/UserNameLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <!--用户名输入-->
                <EditText
                    android:id="@+id/UserNameEdit"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="15dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="15dp"
                    android:background="@drawable/translucent_edit"
                    android:hint="输入用户名"
                    android:textSize="24dp"
                    android:singleLine="true" />

            </LinearLayout>

            <!--嵌套线性布局-->
            <LinearLayout
                android:id="@+id/PassWordLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <!--密码输入-->
                <EditText
                    android:id="@+id/PassWordEdit"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="15dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="15dp"
                    android:background="@drawable/translucent_edit"
                    android:hint="输入用户密码"
                    android:textSize="24dp"
                    android:maxLength="16"
                    android:singleLine="true"
                    android:inputType="textPassword" />

            </LinearLayout>

            <!--嵌套线性布局-->
            <LinearLayout
                android:id="@+id/LayoutButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <!--登录按钮-->
                <Button
                    android:id="@+id/LoginButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="15dp"
                    android:layout_margin="15dp"
                    android:layout_weight="1"
                    android:textColor="@color/cardview_light_background"
                    android:background="@drawable/translucent_button"
                    android:text="登   录"
                    android:textSize="24dp"
                    android:onClick="login"/>

                <!--注册按钮-->
                <Button
                    android:id="@+id/SignUpButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="15dp"
                    android:layout_margin="15dp"
                    android:layout_weight="1"
                    android:textColor="@color/cardview_light_background"
                    android:background="@drawable/translucent_button"
                    android:text="注   册"
                    android:textSize="24dp" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

posted @ 2024-03-31 21:39  艾鑫4646  阅读(9)  评论(0编辑  收藏  举报