2024.4.29

一点都不难發表於2024-06-28

今天完成了註冊介面的設計
Register.java
package com.example.share;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

//import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.share.R;
import com.example.share.dao.UserDao;
import com.example.share.entity.MyUser;
import com.example.share.entity.User;
import com.example.share.utils.CommonUtils;

public class register extends AppCompatActivity implements View.OnClickListener {

private Button SignUpButton,btn_change;
private EditText UserNameEdit,PassWordEdit;
private Handler mainHandler;
private UserDao userDao;
protected static final int CHOOSE_PICTURE = 0;
protected static final int TAKE_PICTURE = 1;
private static final int CROP_SMALL_PICTURE = 2;
protected static Uri tempUri;
private ImageView iv_personal_icon;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

// EdgeToEdge.enable(this);
setContentView(R.layout.activity_register);
mainHandler=new Handler(getMainLooper());
userDao=new UserDao();

    SignUpButton = findViewById(R.id.SignUpButton);
    btn_change = findViewById(R.id.btn_change);
    UserNameEdit = findViewById(R.id.UserNameEdit);
    PassWordEdit = findViewById(R.id.PassWordEdit);

    SignUpButton.setOnClickListener(this);
    btn_change.setOnClickListener(this);

    iv_personal_icon =findViewById(R.id.iv_personal_icon);
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.SignUpButton) {
        String name=UserNameEdit.getText().toString().trim();
        String password=PassWordEdit.getText().toString().trim();

        new Thread(new Runnable() {
            @Override
            public void run() {
                    MyUser myUser =new MyUser(name,password);
                int iRow=0;
                iRow=userDao.addMyUser(myUser);
                Intent intent=new Intent(register.this, MainActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("name",name);
                intent.putExtras(bundle);
                startActivity(intent);

                if(iRow>0){
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            CommonUtils.showShortMsg(register.this,"註冊成功");
                        }
                    });
                }
                else{
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            CommonUtils.showShortMsg(register.this,"註冊失敗");
                            Intent intent=new Intent(getApplicationContext(), MainActivity.class);
                            startActivity(intent);
                        }
                    });
                }
            }

        }).start();
    }
    

}


@Override
public void onPointerCaptureChanged(boolean hasCapture) {
    super.onPointerCaptureChanged(hasCapture);
}

}
activity_register.java

<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"
    android:gravity="center_horizontal">

    <!--Logo-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="30dp" >

        <ImageView
            android:id="@+id/iv_personal_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/default_personal_image" />
    </RelativeLayout>

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btn_change"
        android:layout_marginTop="6dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="選擇頭像" >
    </androidx.appcompat.widget.AppCompatButton>

    <!--巢狀線性佈局-->
    <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">


            <!--註冊按鈕-->
            <androidx.appcompat.widget.AppCompatButton
                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/white"
                android:background="@drawable/translucent_button"
                android:text="注   冊"
                android:textSize="24dp" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>