아무고토 몰라효
[Android] 안드로이드 Activity 이동 2탄 ( feat. Intent, putExtra, getStringExtra ) 본문
[Android] 안드로이드 Activity 이동 2탄 ( feat. Intent, putExtra, getStringExtra )
Always Newbie 2022. 1. 3. 12:57👼🏻 초보 안드로이드 개발자가 매번 구글링하기 싫어서 정리하는 블로그 👼🏻
안녕하세요!
2022.01.03 - [Android/개발] - [Android] 안드로이드 Activity 이동 ( feat. Intent )
[Android] 안드로이드 Activity 이동 ( feat. Intent )
👼🏻 초보 안드로이드 개발자가 매번 구글링하기 싫어서 정리하는 블로그 👼🏻 안녕하세요! 오늘은 앱 개발에 있어서 필수적인 (?) 정말 많이 사용하는 Intent 를 사용해볼거에요!! 첫번째 Acti
devziner.tistory.com
저번엔 Intent 기본 사용법을 확인 했는데
이번엔 액티비티 이동간에 값을 전달하는걸 해볼거에요!!!
저번 글에 작성했던 코드를 재 활용할것이니
참고 부탁드립니다!!
영상을 보면 제가 EditText 에 입력한 내용이 두번째 Activity 로 잘 넘어가는게 보이죠?
요고 오늘 해볼꺼에요!
고럼 바로바로 가봅시다!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
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"
tools:context=".MainActivity"
android:gravity="center"
android:padding="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="첫번째 Activity"
android:gravity="center"
/>
<EditText
android:id="@+id/et_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="글씨를 입력해주세요."
android:gravity="center"
/>
<Button
android:id="@+id/btn_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="두번째 Activity 불러오기"
/>
</LinearLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
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"
tools:context=".SecondActivity"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="짜잔!!! \n두번째 Activity"
android:gravity="center"
/>
<TextView
android:id="@+id/tv_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="40dp"
android:textSize="20dp"
android:textStyle="bold"
/>
</LinearLayout>
뷰는 다 그렸고!
이제 코드로 넘어가봅시다!
MainActivity
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
// 전역변수
Button btn_call;
EditText et_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// xml 연결
btn_call = findViewById(R.id.btn_call);
et_text = findViewById(R.id.et_text);
// 버튼 클릭 이벤트
btn_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str = et_text.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("Text", str);
startActivity(intent);
}
});
}
}
intent.putExtra(Key, value);
이게 핵심입니다!!!!
Key :: 값에 대한 이름
value :: 값
이걸 꼭 startActivity 위에 해줘야해요!
안그러면 intent 가 저걸 못가져가요!
이제 값을 보내줬으니
값을 받아줘야겠죠?
SecondActivity
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
// 전역변수
TextView tv_text;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// xml 연결
tv_text = findViewById(R.id.tv_text);
// Intent!!!!!
Intent intent = getIntent(); // startActivity 를 했으므로 그 intent 를 가져온다는 뜻.
str = intent.getStringExtra("Text"); // putExtra 를 가져온다
tv_text.setText("첫번째 Activity 에서 가져온 글씨는 \n" + str);
}
}
제가 이전 발행글엔 SecondActivity 에 뭐 없어서 아예 코드도 안올렸는데
이번엔 달라요!
MainActivity 에서 값을 보냈으니
SecondActivity 에서 값을 받아주는데 그 방법은
주석에 적힌 것 처럼 받아오는 것 입니다!
getStringExtra 는 말그대로 String 형태의 값이기때문에 저 메소드를 써준거구요
만약 value 가 int , boolean 등 타입이 다르다면
getIntExtra , getBooleanExtra 등 타입별로 받아줘야합니다!
이로써 액티비티간에 값 전달하는 것 까지 완료!!!!
다음에 또 만나욥!
감사합니다!
'Android ( JAVA ) > 개발' 카테고리의 다른 글
[Android] 안드로이드 TabLayout + Fragment ( feat.Adapter ) (0) | 2022.01.05 |
---|---|
[Android] 안드로이드 BottomSheet ( feat.Fragment ) (0) | 2022.01.04 |
[Android] 안드로이드 Activity 이동 ( feat. Intent ) (0) | 2022.01.03 |
[Android] 안드로이드 AlertDialog 2탄 ( +RadioButton ) (0) | 2021.12.31 |
[Android] 안드로이드 AlertDialog (0) | 2021.12.31 |