個人作業登入+註冊

艾鑫4646發表於2024-03-31
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>

相關文章