728x90
반응형
🔸 MainActivity
- Intent 객체 생성
- putExtra로 name과 value값 지정
- startActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("data", "문자열"); startActivity(intent);
🔸SecondActivity
- getIntent로 인텐트를 가져온다.
- getStringExtra로 name을 가져온다.
Intent intent = getIntent(); String str = intent.getStringExtra("data");
📃버튼을 누르면 SecondActivity에 데이터가 출력되도록 하시오.
📝activity_main.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"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_explicit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickExplicit"
android:text="액티비티 실행" />
</LinearLayout>
📝MainActivity.java
package com.example.intent01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickExplicit(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("data", "moble");
startActivity(intent);
}
}
📝activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c3f5e8"
android:gravity="center"
android:text="두번째 액티비티"
android:textAppearance="?android:voiceLanguage"
android:textSize="40sp" />
</RelativeLayout>
📝SecondActivity.java
package com.example.intent01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
String a = intent.getStringExtra("data");
TextView textView = findViewById(R.id.textView);
textView.setText(a);
}
}
📃EditText에 입력한 데이터가 SecondActivity에 출력되게 하시오.
📝activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="20dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et"
android:layout_marginTop="10dp"
android:backgroundTint="#9999ff"
android:text="OK"
android:textSize="30sp"
android:onClick="onClickExplicit"/>
</RelativeLayout>
📝MainActivity.java
package com.example.intent02;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickExplicit(View v) {
EditText et = findViewById(R.id.et);
String str = et.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("data", str + "을/를 입력했습니다.");
startActivity(intent);
}
}
📝activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9999ff"
android:gravity="center"
android:textColor="@color/black"
android:textSize="40sp"
android:id="@+id/tv"/>
</RelativeLayout>
📝SecondActivity.java
package com.example.intent01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
String a = intent.getStringExtra("data");
TextView textView = findViewById(R.id.textView);
textView.setText(a);
}
}
📃리스트뷰에서 해당 아이템을 클릭할 때마다 아이템 안에 있는 데이터 값을 SecondActivity에서 출력되게 하시오.
📝activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"/>
</RelativeLayout>
📝MainActivity.java
package com.example.intent03;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
String str_items[] = {"한국", "중국", "일본"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_list_item_1, str_items);
ListView lv = findViewById(R.id.lv);
lv.setAdapter(aa);
lv.setOnItemClickListener(lis);
}
AdapterView.OnItemClickListener lis = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("data", str_items[position]+"");
startActivity(intent);
}
};
}
📝activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="40sp"
android:id="@+id/tv"/>
</RelativeLayout>
📝SecondActivity.java
package com.example.intent03;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
String str = intent.getStringExtra("data");
((TextView)findViewById(R.id.tv)).setText(str);
}
}
728x90
반응형
'App > Android Studio' 카테고리의 다른 글
Android Studio - 암시적 인텐트 (0) | 2022.08.09 |
---|---|
8강 이상형 월드컵 📌수정중 (0) | 2022.08.08 |
Android Studio - 액티비티 수명주기와 재실행 (0) | 2022.08.08 |
Android Studio - 리스트뷰 아이템에 아이콘 그리기 (0) | 2022.08.05 |
Android Studio - ImageView Shuffle 실습 (0) | 2022.08.04 |
댓글