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

This file was deleted.

19 changes: 0 additions & 19 deletions BCSD_Assignment_2/app/src/main/res/layout/activity_main.xml

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions assignment_week_6/.idea/deploymentTargetSelector.xml

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.

6 changes: 6 additions & 0 deletions assignment_week_6/.idea/kotlinc.xml

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.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
android:supportsRtl="true"
android:theme="@style/Theme.Test"
tools:targetApi="31">
<activity
android:name=".SecondActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.example.test

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

private var count = 0
private lateinit var countTextView: TextView

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 {
showAlertDialog()
}

countButton.setOnClickListener {
count++
countTextView.text = count.toString()
}

randomButton.setOnClickListener {
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, SecondFragment.newInstance(count))
.addToBackStack(null)
.commit()
}
}

private fun showAlertDialog() {
val builder = AlertDialog.Builder(this)
builder.setMessage("AlertDialog")
.setPositiveButton("Positive") { dialog, _ ->
count = 0
countTextView.text = count.toString()
dialog.dismiss()
}
.setNeutralButton("Neutral") { dialog, _ ->
Toast.makeText(this, "Toast 메시지 출력", Toast.LENGTH_SHORT).show()
dialog.dismiss()
}
.setNegativeButton("Negative") { dialog, _ ->
dialog.dismiss()
}
builder.create().show()
}
}
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,58 @@
package com.example.test

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.activity.OnBackPressedCallback

class SecondFragment : Fragment() {

private var count: Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
count = it.getInt(ARG_COUNT)

Choose a reason for hiding this comment

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

null 가능성이 없는 상황에서는
count = requireArguments().getInt(ARG_COUNT)
이런 식으로 써주는 게 좋아용

}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_second, container, false)

val randomValue = (0..count).random()
val randomTextView: TextView = view.findViewById(R.id.randomTextView)

Choose a reason for hiding this comment

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

view binding 적용해보시면 좋을 것 같아요!

randomTextView.text = randomValue.toString()

val backButton: Button = view.findViewById(R.id.backButton)
backButton.setOnClickListener {
requireActivity().supportFragmentManager.popBackStack()
}

requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
requireActivity().supportFragmentManager.popBackStack()
}
})

return view
}

companion object {
private const val ARG_COUNT = "count"

@JvmStatic
fun newInstance(count: Int) =
SecondFragment().apply {
arguments = Bundle().apply {
putInt(ARG_COUNT, count)
}
}
}
}
55 changes: 55 additions & 0 deletions assignment_week_6/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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.fragment.app.FragmentContainerView
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/randomButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
28 changes: 28 additions & 0 deletions assignment_week_6/app/src/main/res/layout/activity_second.xml
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>
28 changes: 28 additions & 0 deletions assignment_week_6/app/src/main/res/layout/fragment_second.xml
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=".SecondFragment">

<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="뒤로가기"
app:layout_constraintTop_toBottomOf="@id/randomTextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
File renamed without changes.
File renamed without changes.
File renamed without changes.