-
Notifications
You must be signed in to change notification settings - Fork 0
6주차 과제 PR #3
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?
6주차 과제 PR #3
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,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) | ||
| } | ||
| } | ||
|
|
||
| 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 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> |
| 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> |
| 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> |
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.
null 가능성이 없는 상황에서는
count = requireArguments().getInt(ARG_COUNT)이런 식으로 써주는 게 좋아용