버전 확인
minSdkVersion, targetSdkVersion
Main 레이아웃 설정
이메일, 패스워드, 작은 버튼 2개 만들기
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <?xml version= "1.0" encoding= "utf-8" ?> <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:paddingBottom= "@dimen/activity_vertical_margin" android:paddingLeft= "@dimen/activity_horizontal_margin" android:paddingRight= "@dimen/activity_horizontal_margin" android:paddingTop= "@dimen/activity_vertical_margin" android:gravity= "center" tools:context= "com.ktds.cocomo.myapp.MainActivity" > <EditText android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:inputType= "textEmailAddress" android:ems= "10" android:id= "@+id/etEmail" android:hint= "Email" android:layout_alignParentTop= "true" android:layout_alignParentLeft= "true" android:layout_alignParentStart= "true" /> <EditText android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:inputType= "textPassword" android:hint= "Password" android:ems= "10" android:id= "@+id/etPassword" android:layout_below= "@+id/etEmail" android:layout_alignParentLeft= "true" android:layout_alignParentStart= "true" android:layout_marginTop= "33dp" /> <Button style= "?android:attr/buttonStyleSmall" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "회원가입" android:id= "@+id/btnRegist" android:layout_below= "@+id/etPassword" android:layout_alignParentLeft= "true" android:layout_alignParentStart= "true" android:layout_marginTop= "26dp" /> <Button style= "?android:attr/buttonStyleSmall" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "로그인" android:id= "@+id/btnLogin" android:layout_alignBottom= "@+id/btnRegist" android:layout_toRightOf= "@+id/btnRegist" android:layout_toEndOf= "@+id/btnRegist" /> </RelativeLayout> |
회원가입 화면
RegistActivity 추가
회원가입 화면 만들기
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <? xml version = "1.0" encoding = "utf-8" ?> android:orientation = "vertical" android:gravity = "center" android:layout_width = "match_parent" android:layout_height = "match_parent" android:paddingBottom = "@dimen/activity_vertical_margin" android:paddingLeft = "@dimen/activity_horizontal_margin" android:paddingRight = "@dimen/activity_horizontal_margin" android:paddingTop = "@dimen/activity_vertical_margin" tools:context = "com.ktds.cocomo.myapp.RegistActivity" > < EditText android:layout_width = "match_parent" android:layout_height = "wrap_content" android:inputType = "textEmailAddress" android:ems = "10" android:hint = "Email" android:id = "@+id/etEmail" /> < EditText android:layout_width = "match_parent" android:layout_height = "wrap_content" android:inputType = "textPassword" android:ems = "10" android:hint = "Password" android:id = "@+id/etPassword" /> < EditText android:layout_width = "match_parent" android:layout_height = "wrap_content" android:inputType = "textPassword" android:ems = "10" android:hint = "Password" android:id = "@+id/etPasswordConfirm" /> < LinearLayout android:orientation = "horizontal" android:gravity = "center" android:layout_width = "match_parent" android:layout_height = "wrap_content" > < Button android:id = "@+id/btnDone" android:text = "가입" android:layout_weight = "1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> < Button android:id = "@+id/btnCancel" android:text = "취소" android:layout_weight = "1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> </ LinearLayout > </ LinearLayout > |
메인에서 회원가입 버튼 눌렀을 때의 이벤트
startActivityForResult 이용
1000은 요청에 대한 식별자
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class MainActivity extends AppCompatActivity { private Button btnRegist; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnRegist = (Button) findViewById(R.id.btnRegist); btnRegist.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), RegistActivity. class ); startActivityForResult(intent, 1000 ); } }); } } |
회원가입 눌렀을 때 RegistActivity에서 처리
이메일, 패스워드 입력하고 확인 누르면 RegistActivity 사라지면서 MainActivity로 이메일을 보내고
MainActivity에서는 이메일을 받아서 Email 입력 칸에 데이터를 넣는다.
취소를 누르면 그냥 MainActivity로 이동한다.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | package com.ktds.cocomo.myapp; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class RegistActivity extends AppCompatActivity { private EditText etEmail; private EditText etPassword; private EditText etPasswordConfirm; private Button btnDone; private Button btnCancel; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_regist); etEmail = (EditText) findViewById(R.id.etEmail); etPassword = (EditText) findViewById(R.id.etPassword); etPasswordConfirm = (EditText) findViewById(R.id.etPasswordConfirm); btnDone = (Button) findViewById(R.id.btnDone); btnCancel = (Button) findViewById(R.id.btnCancel); // 비밀번호 일치 검사 etPasswordConfirm.addTextChangedListener( new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String password = etPassword.getText().toString(); String confirm = etPasswordConfirm.getText().toString(); if ( password.equals(confirm) ) { etPassword.setBackgroundColor(Color.GREEN); etPasswordConfirm.setBackgroundColor(Color.GREEN); } else { etPassword.setBackgroundColor(Color.RED); etPasswordConfirm.setBackgroundColor(Color.RED); } } @Override public void afterTextChanged(Editable s) { } }); btnDone.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // 이메일 입력 확인 if ( etEmail.getText().toString().length() == 0 ) { Toast.makeText(RegistActivity. this , "Email을 입력하세요!" , Toast.LENGTH_SHORT).show(); etEmail.requestFocus(); return ; } // 비밀번호 입력 확인 if ( etPassword.getText().toString().length() == 0 ) { Toast.makeText(RegistActivity. this , "비밀번호를 입력하세요!" , Toast.LENGTH_SHORT).show(); etPassword.requestFocus(); return ; } // 비밀번호 확인 입력 확인 if ( etPasswordConfirm.getText().toString().length() == 0 ) { Toast.makeText(RegistActivity. this , "비밀번호 확인을 입력하세요!" , Toast.LENGTH_SHORT).show(); etPasswordConfirm.requestFocus(); return ; } // 비밀번호 일치 확인 if ( !etPassword.getText().toString().equals(etPasswordConfirm.getText().toString()) ) { Toast.makeText(RegistActivity. this , "비밀번호가 일치하지 않습니다!" , Toast.LENGTH_SHORT).show(); etPassword.setText( "" ); etPasswordConfirm.setText( "" ); etPassword.requestFocus(); return ; } Intent result = new Intent(); result.putExtra( "email" , etEmail.getText().toString()); // 자신을 호출한 Activity로 데이터를 보낸다. setResult(RESULT_OK, result); finish(); } }); btnCancel.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } } |
MainActivity에서 데이터 받기
Regist에서 보낸 데이터를 받는다.
Regist에서 입력한 이메일을 Email창에 입력한다.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | public class MainActivity extends AppCompatActivity { private EditText etEmail; private Button btnRegist; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); etEmail = (EditText) findViewById(R.id.etEmail); btnRegist = (Button) findViewById(R.id.btnRegist); btnRegist.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), RegistActivity. class ); // SINGLE_TOP : 이미 만들어진게 있으면 그걸 쓰고, 없으면 만들어서 써라 intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // 동시에 사용 가능 // intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); // intent를 보내면서 다음 액티비티로부터 데이터를 받기 위해 식별번호(1000)을 준다. startActivityForResult(intent, 1000 ); } }); } @Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { super .onActivityResult(requestCode, resultCode, data); // setResult를 통해 받아온 요청번호, 상태, 데이터 Log.d( "RESULT" , requestCode + "" ); Log.d( "RESULT" , resultCode + "" ); Log.d( "RESULT" , data + "" ); if (requestCode == 1000 && resultCode == RESULT_OK) { Toast.makeText(MainActivity. this , "회원가입을 완료했습니다!" , Toast.LENGTH_SHORT).show(); etEmail.setText(data.getStringExtra( "email" )); } } } |
회원가입창에서 데이터를 입력한 뒤 가입 버튼을 누릅니다.
첫 번째 액티비티로 데이터를 가지고 이동한 것을 확인하실 수 있습니다.