안드로이드스튜디오 다른 Activity로 값 넘겨주기 (intent, bundle)

2019. 12. 19. 15:432020/Android App Develop

( *이전 게시글에서 작성한 xml과 같은 코드이지만 xml파일들의 이름을 변경하였음 ) 

 

Scenario

    activity_login.xml - 로그인 화면 - matching with MainActivity.class 

    activity_login_result.xml - 로그인 결과값 출력화면 - matching with LoginResultActivity.class 

    [1] 사용자가 login화면을 오픈 (activity_login.xml)

    [2] email과 password입력 후 login 터치 (MainActivity.class에서 터치 감지 -> 값들과 함께 LoginResultActivity.class이동)

    [3] activity_login_result.xml을 통해서 사용자에게 결과값이 보여진다

 

* 이전 게시글에서 사용했었던 TExtInputEditText 컴포넌트를 사용하려고 했지만 오류발생으로 EditText 대체하였다 (오류이유 모름 )

[ MainActivity.class ]

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
package com.example.myapplication;
 
 
 
public class MainActivity extends AppCompatActivity {
 
// 각 객체들을 저장하기 위해 변수 선언
    EditText TextInputEditText_email;
    EditText TextInputEditText_password;
    RelativeLayout RelativeLayout_loginButton;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
 
// findViewById 메서드를 통해 activity_login.xml에서 요소를 가져온다 ( id는 xml에서 지정 )
// 객체는 변수에 저장되어진다.
        TextInputEditText_email = findViewById(R.id.TextInputEditText_email);
        TextInputEditText_password =  findViewById(R.id.TextInputEditText_password);
        RelativeLayout_loginButton = findViewById(R.id.RelativeLayout_LoginButton);
 
 
// 해당 객체를 클릭이 가능케하고, 터치를 감지한다
 
        RelativeLayout_loginButton.setClickable(true);
        RelativeLayout_loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
           public void onClick(View v) {
 
// 변수에 저장된 객체에서 Text를 가져오고 String형식으로 변환 후 저장
 
                String email = TextInputEditText_email.getText().toString();
                String password = TextInputEditText_password.getText().toString();
 
// Activity사이에서 값을 전달하기 위해서는 intent를 사용한다
// intent생성시 현재 activity와 이동할 activity선언
// putExtra 메서드를 통해 키 값과 데이터를 저장
 
                Intent intent = new Intent(getBaseContext(),LoginResultActivity.class);
                intent.putExtra("email",email);
                intent.putExtra("password",password);
 
               startActivity(intent); // intent와 함께 다음 activity실행
 
            }
        });
 
 
 
    }
 
 
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

[ LoginResultActivity.class ]

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
package com.example.myapplication;
 
 
 
public class LoginResultActivity extends AppCompatActivity {
 
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_result);
 
        TextView = findViewById(R.id.TextView_loginResult);
 
// 넘어온 값을 받기위해 intent객체를 생성하지만 getIntent()를 통해 넘어온 intent객체를 받아온다
 
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras(); // bundle을 통해 Extra들을 모두 가져온다
        String email = bundle.getString("email"); // 키 값을 통해서 extras에 있는 값들을 얻는다.
        String password = bundle.getString("password"); // 해당 값들은 변수에 저장
 
        TextView.setText(email + " / " + password); // LoginResult.xml에 있는 객체에 Text를 설정
 
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

[ activity_login.xml 소스코드 일부 ]

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/TextInputEditText_email"
android:hint="Email"></EditText>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/TextInputEditText_password"
android:hint="password"></EditText>

<RelativeLayout
android:id="@+id/RelativeLayout_LoginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/botton_background_blue"
android:clickable="true"
android:gravity="center">

<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:layout_toRightOf="@+id/login_textView_1"
android:src="@drawable/yellow_star"></ImageView>

<TextView
android:id="@+id/login_textView_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@null"
android:text="LOGIN"
android:textColor="#ffffff"
android:textSize="15dp"></TextView>

</RealativeLayout>

[ activity_login_result.xml 소스코드 일부 ]

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hello"
android:id="@+id/TextView_loginResult"
></TextView>

 

=> 아이디를 지정해줌으로써 xml에 위치한 해당 요소들을 찾아낼 수 있다 => 여러가지 기능을 적용시킬 수 있음

[ 실행결과 ]

 

 

 

 

2019/12/17 - [2019.12/Android App Develop] - 안드로이드 스튜디오 로그인 화면 구성하기 ( textInputEditText, Button, ImageView ) (android x implementat

2019/12/16 - [2019.12/Android App Develop] - 안드로이드 스튜디오 간단한 로그인화면 구성하기