Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true" >
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:exported="true">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

외부에 노출될 필요가 없는 Activity는
exported="false" 로 해주세요.

</activity>
</application>

</manifest>
37 changes: 37 additions & 0 deletions app/src/main/java/com/example/android_25_2/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package com.example.android_25_2

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import kotlin.random.Random

class MainActivity : AppCompatActivity() {
private lateinit var launcher: ActivityResultLauncher<Intent>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
Expand All @@ -16,5 +25,33 @@ class MainActivity : AppCompatActivity() {
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

과제에
d. 뒤로가기를 하면 첫번째 Activity으로 돌아오고, 화면의 숫자가 두번째 Activity의 숫자로 변경됩니다.
요건에 충족되지 않습니다.
SecondAcitivity 에서 setResult 까진 하셨는데 이 부분을 빼먹으셨네요.


val textView: TextView = findViewById(R.id.textView_main)

val toastButton: Button = findViewById(R.id.button_toast)
toastButton.setOnClickListener {
Toast.makeText(this, getString(R.string.toast_message), Toast.LENGTH_SHORT).show()
}

val countButton: Button = findViewById(R.id.button_count)
countButton.setOnClickListener {
val currentText1 = textView.text.toString().toInt()
val newCount = currentText1 + 1
textView.text = newCount.toString()
}

val randomButton: Button = findViewById(R.id.button_random)
randomButton.setOnClickListener {
val randomNumber = Random.nextInt(0, 15)
Copy link
Collaborator

@KYM-P KYM-P Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

과제 조건에
C. Random 버튼을 누르면 랜덤한 숫자를 출력하는 두번째 Activity로 이동합니다.
0부터 첫번째 화면의 숫자 사이의 랜덤한 숫자가 출력됩니다.
요건에 충족되지 않았습니다.

Random.nextInt(0,15) 로 0~14 사이의 랜덤한 숫자가 아닌
Random.nextInt(0, textView.text.toInt()) 를 통해 random 범위를 정하셔야 합니다.

launchSecondActivity(randomNumber)
}
}

private fun launchSecondActivity(randomNumber: Int) {
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("random_number", randomNumber)
launcher.launch(intent)
}
}
43 changes: 43 additions & 0 deletions app/src/main/java/com/example/android_25_2/SecondActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.android_25_2

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.result.registerForActivityResult
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import kotlin.random.Random

class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.Second)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
Log.e("MYLOG","done3")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

디버그용 로그는 지워주세요

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 질문: Log.v, Log.d, Log.e의 차이는 무엇일까요

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log.v는 비교적 세세하며 Log.d는 디버깅용 로그입니다 Log.e는 에러 로그입니다


val textView: TextView = findViewById(R.id.textView_main)
val randomButton: Button = findViewById(R.id.button_random)

val receivedRandomNumber = intent.getIntExtra("random_number", 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"random_number" 로 내부 str 선언이 아닌
const 변수로 하나의 객체를 참조해서 사용하는게 훨씬 안전합니다.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가로 상수 처리해도 괜찮을 것 같네요

textView.text = receivedRandomNumber.toString()

randomButton.setOnClickListener {
val randomNumber = Random.nextInt(0, 15)
Copy link
Collaborator

@KYM-P KYM-P Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

마찬가지 0,14 의 랜덤값이 아닙니다.
물론 랜덤 함수를 생성하는 버튼은 과제에 없어도 되는 요소입니다.

textView.text = randomNumber.toString()
}

val resultIntent = Intent()
resultIntent.putExtra("random_number", textView.text)
setResult(Activity.RESULT_OK, resultIntent)
}
}
Binary file added app/src/main/res/drawable/ic_logo_google.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/ic_logo_kakao.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/ic_logo_naver.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 54 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp"
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="250dp"/>

<Button
android:id="@+id/button_count"
android:layout_width="80dp"
android:layout_height="35dp"
android:text="@string/message_button_count"
android:textSize="9dp"
android:backgroundTint="@color/purple_200"
app:layout_constraintTop_toBottomOf="@+id/textView_main"
android:layout_marginTop="200dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

<Button
android:id="@+id/button_toast"
android:layout_width="80dp"
android:layout_height="35dp"
android:text="@string/message_button_toast"
android:textSize="9dp"
android:backgroundTint="@color/purple_200"
app:layout_constraintTop_toBottomOf="@+id/textView_main"
android:layout_marginTop="200dp"
android:layout_marginEnd="180dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

<Button
android:id="@+id/button_random"
android:layout_width="80dp"
android:layout_height="35dp"
android:text="@string/message_button_random"
android:textSize="9dp"
android:backgroundTint="@color/purple_200"
app:layout_constraintTop_toBottomOf="@+id/textView_main"
android:layout_marginTop="200dp"
android:layout_marginStart="180dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
62 changes: 62 additions & 0 deletions app/src/main/res/layout/activity_second.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple_500"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".SecondActivity">

<TextView
android:id="@+id/textView_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp"
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="250dp"/>

<Button
android:id="@+id/button_count"
android:layout_width="80dp"
android:layout_height="35dp"
android:text="@string/message_button_count"
android:textSize="9dp"
android:backgroundTint="@color/purple_200"
app:layout_constraintTop_toBottomOf="@+id/textView_main"
android:layout_marginTop="200dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

<Button
android:id="@+id/button_toast"
android:layout_width="80dp"
android:layout_height="35dp"
android:text="@string/message_button_toast"
android:textSize="9dp"
android:backgroundTint="@color/purple_200"
app:layout_constraintTop_toBottomOf="@+id/textView_main"
android:layout_marginTop="200dp"
android:layout_marginEnd="180dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

<Button
android:id="@+id/button_random"
android:layout_width="80dp"
android:layout_height="35dp"
android:text="@string/message_button_random"
android:textSize="9dp"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

textSize 는 sp 가 더 좋습니다.
sp 는 절대적 크기가 아닌 사용자 폰트 크기에 맞춰지거든요

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안드로이드에서 사용하는 길이 단위에 대해 알아보시면 좋을 것 같습니다

android:backgroundTint="@color/purple_200"
app:layout_constraintTop_toBottomOf="@+id/textView_main"
android:layout_marginTop="200dp"
android:layout_marginStart="180dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="blue">#1E90FF</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<resources>
<string name="app_name">Android_25-2</string>
<string name="message_button_count">"COUNT"</string>
<string name="message_button_toast">"TOAST"</string>
<string name="message_button_random">"RANDOM"</string>
<string name="toast_message">"Boink"</string>
</resources>