Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Archives
Today
Total
관리 메뉴

쨍쨍

#2. AI Smart Home_RegisterActivity 본문

프로그래밍 코드/Android Studio

#2. AI Smart Home_RegisterActivity

이선선 2024. 3. 13. 03:03

[ 회원가입 화면 ]

 

 

회원가입 화면 UI

 

 

[ activity_register.xml ]

 

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        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="vertical"
            android:background="@color/Primary">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="회원가입"
                android:textColor="#ffffff"
                android:textSize="30dp"
                android:textStyle="bold"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"/>

        </LinearLayout>

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

            <LinearLayout
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="100dp"
                android:layout_gravity="center">

                <EditText
                    android:id="@+id/idText"
                    android:layout_width="190dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginRight="10dp"
                    android:background="@color/Primary"
                    android:drawableStart="@drawable/baseline_person_24"
                    android:drawableLeft="@drawable/baseline_person_24"
                    android:hint="아이디"
                    android:textColor="#ffffff"
                    android:textColorHint="#ffffff" />

                <Button
                    android:id="@+id/validateButton"
                    android:layout_width="80dp"
                    android:layout_height="25dp"
                    android:layout_gravity="center"
                    android:background="@color/validateButton"
                    android:text="중복체크"
                    android:textStyle="bold"
                    android:textColor="#ffffff"
                    android:textSize="10dp" />

            </LinearLayout>

            <EditText
                android:id="@+id/passwordText"
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="15dp"
                android:background="@color/Primary"
                android:drawableStart="@drawable/baseline_https_24"
                android:drawableLeft="@drawable/baseline_https_24"
                android:hint="비밀번호"
                android:inputType="textPassword"
                android:textColor="#ffffff"
                android:textColorHint="#ffffff" />

            <EditText
                android:id="@+id/confirmPasswordText"
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="15dp"
                android:background="@color/Primary"
                android:drawableStart="@drawable/baseline_https_24"
                android:drawableLeft="@drawable/baseline_https_24"
                android:hint="비밀번호"
                android:inputType="textPassword"
                android:textColor="#ffffff"
                android:textColorHint="#ffffff" />

            <EditText
                android:id="@+id/nameText"
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="15dp"
                android:background="@color/Primary"
                android:drawableStart="@drawable/baseline_https_24"
                android:drawableLeft="@drawable/baseline_https_24"
                android:hint="이름"
                android:inputType="textPassword"
                android:textColor="#ffffff"
                android:textColorHint="#ffffff" />

            <EditText
                android:id="@+id/phoneText"
                android:layout_width="280dp"
                android:hint="전화번호"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="15dp"
                android:background="@color/Primary"
                android:drawableStart="@drawable/baseline_mail_outline_24"
                android:drawableLeft="@drawable/baseline_mail_outline_24"
                android:inputType="textPassword"
                android:textColor="#ffffff"
                android:textColorHint="#ffffff" />

            <RadioGroup
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:id="@+id/genderGroup"
                android:layout_gravity="center_horizontal"
                android:orientation="horizontal"
                android:layout_marginTop="15dp">

                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="여성"
                    android:id="@+id/genderWoman"
                    android:layout_weight="1"
                    android:checked="true"/>

                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="남성"
                    android:id="@+id/genderMan"
                    android:layout_weight="1"/>

            </RadioGroup>

            <Spinner
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="15dp"
                android:id="@+id/AptSpinner"></Spinner>

            <Button
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="30dp"
                android:background="@color/Button"
                android:text="가입하기"
                android:textColor="#ffffff"
                android:textSize="20dp"
                android:textStyle="bold"
                android:id="@+id/registerButton"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="18dp"
                android:layout_marginTop="20dp"
                android:text="처음화면으로 돌아가기"
                android:id="@+id/backButton"/>


        </LinearLayout>


    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

 

 

[ RegisterActivity.java ]

 

 

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class  RegisterActivity extends AppCompatActivity implements SocketActivity.OnDataReceivedListener {

    private EditText usernameEditText, nameEditText, phoneEditText, passwordEditText;
    private Button registerButton, validateButton;
    private TextView backButton;
    private SocketActivity socketActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        usernameEditText = findViewById(R.id.idText);
        nameEditText = findViewById(R.id.nameText);
        phoneEditText = findViewById(R.id.phoneText);
        passwordEditText = findViewById(R.id.passwordText);
        registerButton = findViewById(R.id.registerButton);
        validateButton = findViewById(R.id.validateButton);
        backButton = findViewById(R.id.backButton); // backButton 추가

        socketActivity = SocketActivity.getInstance();
        socketActivity.connectToServer(this);

        // "처음화면으로 돌아가기" 버튼 클릭 시 LoginActivity로 이동
        backButton.setOnClickListener(view -> {
            // 소켓 연결 해제
            SocketActivity.getInstance().disconnect();

            // 로그인 화면을 초기 상태로 열기 위한 Intent 생성
            Intent intent = new Intent(RegisterActivity.this, LoginActivity2.class);
            // 기존 액티비티 스택을 비우고 새 작업으로 시작
            startActivity(intent);
            finish(); // 회원가입 액티비티 종료
        });

        validateButton.setOnClickListener(view -> {
            String username = usernameEditText.getText().toString();
            if (!username.isEmpty()) {
                socketActivity.sendUsernameValidationRequest(username);
                validateButton.setEnabled(false);
            } else {
                Toast.makeText(this, "아이디를 입력하세요.", Toast.LENGTH_SHORT).show();
            }
        });

        registerButton.setOnClickListener(view -> {
            String username = usernameEditText.getText().toString();
            String name = nameEditText.getText().toString();
            String phone = phoneEditText.getText().toString();
            String password = passwordEditText.getText().toString();

            // 유효성 검사: 빈 값이 있는지 체크
            if (username.isEmpty() || name.isEmpty() || phone.isEmpty() || password.isEmpty()) {
                Toast.makeText(this, "모든 필드를 입력하세요.", Toast.LENGTH_SHORT).show();
            } else {
                // 유효성 검사 통과 시 서버로 데이터 전송
                socketActivity.sendSignupData(username, name, phone, password);
                registerButton.setEnabled(false); // 회원가입 버튼 비활성화
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        socketActivity.disconnect();
    }

@Override
protected void onResume() {
    super.onResume();
    // onResume에서 불필요한 소켓 연결을 방지
    if (!socketActivity.isSocketConnected()) {
        socketActivity.connectToServer(this);
    }
}

public void onDataReceived(String data) {
    Log.d("RegisterActivity", "Raw data from server: '" + data + "'");

    // 데이터를 표준화 (서버에서 공백 없이 보내온 경우 처리)
    data = data.trim().replaceAll("(?i)registersuccess", "REGISTER SUCCESS");
    Log.d("RegisterActivity", "Processed data received from server: '" + data + "' (length: " + data.length() + ")");

    String finalData = data;
    runOnUiThread(() -> {
        Log.d("RegisterActivity", "Comparing data: '" + finalData + "'");

        if (finalData.equalsIgnoreCase("VALIDATE SUCCESS")) {
            Toast.makeText(RegisterActivity.this, "사용 가능한 아이디입니다.", Toast.LENGTH_SHORT).show();
            validateButton.setEnabled(true);
        } else if (finalData.equalsIgnoreCase("VALIDATE FAILURE")) {
            Toast.makeText(RegisterActivity.this, "이미 사용 중인 아이디입니다.", Toast.LENGTH_SHORT).show();
            validateButton.setEnabled(true);
        } else if (finalData.equalsIgnoreCase("REGISTER SUCCESS")) {
            Toast.makeText(RegisterActivity.this, "회원가입이 완료되었습니다.", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(RegisterActivity.this, LoginActivity2.class);
            startActivity(intent);
            finish();
        } else if (finalData.equalsIgnoreCase("REGISTER FAILURE: Invalid Registration Data")) {
            Toast.makeText(RegisterActivity.this, "회원가입에 실패했습니다. 입력한 정보를 확인하세요.", Toast.LENGTH_SHORT).show();
            // 추가적인 오류 메시지 또는 처리 로직을 여기에 추가할 수 있습니다.
        } else {
            //Toast.makeText(RegisterActivity.this, "알 수 없는 서버 응답: " + finalData, Toast.LENGTH_LONG).show();
            Log.d("RegisterActivity", "알 수 없는 서버 응답: " + finalData);
        }
    });
}
}

 

 

#1. 아이디가 빈칸인 상태에서 중복확인을 눌렀을 때

 

#2. 중복확인을 누르지 않은 채 가입하기를 눌렀을 때