본문 바로가기
(Android App)안드로이드 앱

(Android App)안드로이드 스튜디오:데이터 연동없이 간단한 회원가입,로그인 프로그램 만들기/getExtra,putExtra 활용.

by 공부가싫다가도좋아 2021. 2. 17.
반응형

간단한 회원가입,로그인 프로그램 만들기


#app/manifests/AndroidManifests 

Login화면이 맨 앞에 나오게 하기 위해서 맨 아래에 배치.

 

#activiti__signup.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:orientation="vertical"
    tools:context=".SignupActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Sign up"
            android:textSize="50sp" />
            
//회원가입시 넣을 이름
        <EditText
            android:id="@+id/signup_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:ems="10"
            android:hint="이름"
            android:inputType="textPersonName" />

//회원가입시 넣을 ID
        <EditText
            android:id="@+id/signup_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:ems="10"
            android:hint="ID"
            android:inputType="textPersonName" />

//회원가입시 넣을 Password
        <EditText
            android:id="@+id/signup_pw"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:ems="10"
            android:hint="PASSWORD"
            android:inputType="textPersonName" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:orientation="horizontal">

//뒤로가기 버튼
        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Back"
            android:textSize="18sp" />
            
//회원가입완료 버튼
        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Sign up"
            android:textSize="18sp" />

    </LinearLayout>
</LinearLayout>

 

#SignupActivity.java

package com.example.project;
//import(생략)
public class SignupActivity extends AppCompatActivity {

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

//이름,id,password 주소값 가져오기.
        EditText name=findViewById(R.id.signup_name);
        EditText id=findViewById(R.id.signup_id);
        EditText pw=findViewById(R.id.signup_pw);

//button6의 주소값 가져오고, button6 실행시 로그인 화면으로 돌아감.

        Button button6=findViewById(R.id.button6);
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

//button7실행시 name,id,password의 데이터를 putExtra로 담음.
        Button button7=findViewById(R.id.button7);
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String username=name.getText().toString();
                final String userid=id.getText().toString();
                final String userpw=pw.getText().toString();
                
//이름,id,password의 입력칸이 하나라도 비었을시 알림이 뜸.         
                if(username.equals("")||userid.equals("")||userpw.equals("")){
                    AlertDialog.Builder dialog=new AlertDialog.Builder(SignupActivity.this);
                    dialog.setIcon(R.mipmap.ic_launcher);
                    dialog.setTitle("알림");
                    dialog.setMessage("모두 입력하시오.");
                    dialog.setNegativeButton("확인",null);
                    dialog.show();
                }
 //모두 입력했을시 데이터를 담고 로그인 화면으로 돌아감
                else{
                    Intent intent=new Intent(getApplicationContext(),LoginActivity.class);
                    intent.putExtra("userid",userid);
                    intent.putExtra("userpw",userpw);
                    intent.putExtra("username",username);
                    startActivityForResult(intent,RESULT_OK);

                }


            }
        });

    }
}

 

#activity_login.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:background="#DFD6EF"
    android:orientation="vertical"
    tools:context=".LoginActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.6"
        android:background="#DFD6EF"
        android:orientation="vertical">

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
//화면 타이틀
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="기출문제"
            android:textSize="25sp" />
            
//아이디 입력 칸
        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="180dp"
            android:ems="10"
            android:hint="ID"
            android:inputType="textPersonName" />
            
//패스워드 입력칸
        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="PASSWORD"
            android:inputType="textPassword" />

//로그인 버튼
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="login"
            android:text="Sign in"
            android:textSize="18sp"
            app:backgroundTint="#673AB7" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.4"
        android:orientation="horizontal" >

//회원가입 버튼
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:text="sign up"
            android:textSize="18sp"
            app:backgroundTint="#673AB7" />


    </LinearLayout>

</LinearLayout>

 

#LoginActivity.java

package com.example.project;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {


    public static final int REQUEST_CODE_MAIN=100;
    public static final int REQUEST_CODE_SIGNUP=101;
   

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

        Button button=findViewById(R.id.button);


        Button button2=findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),SignupActivity.class);
                startActivityForResult(intent,REQUEST_CODE_SIGNUP);

            }
        });

    }


//id가 button 인 Sign in 버튼의 onClick에 login을 써줌.
//로그인 함수
    public void login(View v){
        EditText editText=findViewById(R.id.editText);
        EditText editText2=findViewById(R.id.editText2);

//회원가입창에서 받은 데이터를 여기에 넘겨줌
        Intent receiveIntent=getIntent();
        String id=receiveIntent.getStringExtra("userid");
        String pw=receiveIntent.getStringExtra("userpw");
        String name=receiveIntent.getStringExtra("username");

        String userid=editText.getText().toString();
        String userpw=editText2.getText().toString();

//아이디 비밀번호 일치일때는 메인 화면에 진입. 불일치시 토스트 메시지.
        if(userid.equals(id)){
            if(userpw.equals(pw)){
                Intent intent=new Intent(getApplicationContext(),MainActivity.class);

                Toast.makeText(this,name+"님 안녕하세요.",Toast.LENGTH_SHORT).show();
                startActivityForResult(intent,REQUEST_CODE_MAIN);
            }
            else{
                Toast.makeText(getApplicationContext(),"아이디 혹은 비밀번호가 일치하지 않습니다",Toast.LENGTH_SHORT).show();
            }

        }
        else{
            Toast.makeText(getApplicationContext(),"아이디 혹은 비밀번호가 일치하지 않습니다",Toast.LENGTH_SHORT).show();
        }

    }


}
반응형

댓글