-
Notifications
You must be signed in to change notification settings - Fork 0
5주차 과제 PR #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
5주차 과제 PR #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package com.example.test | ||
|
|
||
| import android.content.Intent | ||
| import android.os.Bundle | ||
| import android.widget.Button | ||
| import android.widget.TextView | ||
| import android.widget.Toast | ||
| import androidx.activity.result.contract.ActivityResultContracts | ||
| import androidx.appcompat.app.AppCompatActivity | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| private var count = 0 | ||
| private lateinit var countTextView: TextView | ||
|
|
||
| private val activityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> | ||
| if (result.resultCode == RESULT_OK) { | ||
| val randomValue = result.data?.getIntExtra("randomValue", count) | ||
| count = randomValue ?: count | ||
| countTextView.text = count.toString() | ||
| } | ||
| } | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_main) | ||
|
|
||
| val toastButton: Button = findViewById(R.id.toastButton) | ||
| val countButton: Button = findViewById(R.id.countButton) | ||
| val randomButton: Button = findViewById(R.id.randomButton) | ||
| countTextView = findViewById(R.id.countTextView) | ||
|
|
||
| toastButton.setOnClickListener { | ||
| Toast.makeText(this, "Toast 메시지 출력", Toast.LENGTH_SHORT).show() | ||
| } | ||
|
|
||
| countButton.setOnClickListener { | ||
| count++ | ||
| countTextView.text = count.toString() | ||
| } | ||
|
|
||
| randomButton.setOnClickListener { | ||
| val intent = Intent(this, SecondActivity::class.java) | ||
| intent.putExtra("count", count) | ||
| activityLauncher.launch(intent) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.example.test | ||
|
|
||
| import android.app.Activity | ||
| import android.content.Intent | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import android.os.Bundle | ||
| import android.widget.Button | ||
| import android.widget.TextView | ||
| import kotlin.random.Random | ||
|
|
||
| class SecondActivity : AppCompatActivity() { | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_second) | ||
|
|
||
| val count = intent.getIntExtra("count", 0) | ||
|
|
||
| val randomValue = Random.nextInt(0, count + 1) | ||
|
|
||
| val randomTextView: TextView = findViewById(R.id.randomTextView) | ||
| randomTextView.text = randomValue.toString() | ||
|
|
||
| val backButton: Button = findViewById(R.id.backButton) | ||
| backButton.setOnClickListener { | ||
| val resultIntent = Intent().apply { | ||
| putExtra("randomValue", randomValue) | ||
| } | ||
| setResult(Activity.RESULT_OK, resultIntent) | ||
| finish() | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <androidx.constraintlayout.widget.ConstraintLayout 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"> | ||
|
|
||
| <TextView | ||
| android:id="@+id/countTextView" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="0" | ||
| app:layout_constraintTop_toTopOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| android:textSize="24sp"/> | ||
|
|
||
| <Button | ||
| android:id="@+id/toastButton" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="Toast" | ||
| app:layout_constraintTop_toBottomOf="@id/countTextView" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintEnd_toEndOf="parent"/> | ||
|
|
||
| <Button | ||
| android:id="@+id/countButton" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="Count" | ||
| app:layout_constraintTop_toBottomOf="@id/toastButton" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintEnd_toEndOf="parent"/> | ||
|
|
||
| <Button | ||
| android:id="@+id/randomButton" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="Random" | ||
| app:layout_constraintTop_toBottomOf="@id/countButton" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintEnd_toEndOf="parent"/> | ||
| </androidx.constraintlayout.widget.ConstraintLayout> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <androidx.constraintlayout.widget.ConstraintLayout 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/randomTextView" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:textSize="24sp" | ||
| tools:text="랜덤 번호 생성" | ||
| app:layout_constraintTop_toTopOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintEnd_toEndOf="parent"/> | ||
|
|
||
| <Button | ||
| android:id="@+id/backButton" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="Back" | ||
| app:layout_constraintTop_toBottomOf="@id/randomTextView" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintEnd_toEndOf="parent"/> | ||
|
|
||
| </androidx.constraintlayout.widget.ConstraintLayout> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
registerForActivityResult 따로 빼주셔서 관리 했네요 👍