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
2 changes: 2 additions & 0 deletions .gradle/config.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Tue Nov 26 00:44:34 KST 2024
java.home=C\:\\Program Files\\Android\\Android Studio1\\jbr
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

195 changes: 195 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

android {
namespace = "com.example.assignment02"
compileSdk = 34
compileSdk = 35

defaultConfig {
applicationId = "com.example.assignment02"
Expand Down Expand Up @@ -36,11 +36,11 @@ android {
}

dependencies {
implementation("androidx.activity:activity-ktx:1.7.0")
implementation("androidx.core:core-ktx:1.13.1")

implementation("androidx.core:core-ktx:1.15.0")
implementation("androidx.appcompat:appcompat:1.7.0")
implementation("com.google.android.material:material:1.12.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.constraintlayout:constraintlayout:2.2.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.2.1")
androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RandomActivity" />
</application>

</manifest>
87 changes: 87 additions & 0 deletions app/src/main/java/com/example/assignment02/CountFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.example.assignment02

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 android.widget.Toast
import androidx.fragment.app.Fragment
import android.content.DialogInterface;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

class CountFragment : Fragment() {

private lateinit var numberText: TextView
private var currentNumber: Int = 0

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

numberText = view.findViewById(R.id.number_text)
val countButton = view.findViewById<Button>(R.id.count_btn)
val toastButton = view.findViewById<Button>(R.id.toast_btn)
val randomButton = view.findViewById<Button>(R.id.random_btn)

currentNumber = arguments?.getInt("count") ?: 0
numberText.text = currentNumber.toString()

countButton.setOnClickListener {
currentNumber++
numberText.text = currentNumber.toString()
}
toastButton.setOnClickListener {
showAlertDialog()
}
randomButton.setOnClickListener {
val randomFragment = RandomFragment.newInstance(currentNumber)
requireActivity().supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, randomFragment)
.addToBackStack(null)
.commit()
}

return view
}

private fun showAlertDialog() {
// Fragment에서는 requireContext()를 사용하여 컨텍스트를 가져옴
val builder = AlertDialog.Builder(requireContext())
builder.setTitle("ToastButton")
.setMessage("초기화/Toast/취소")

// Positive 버튼 설정
.setPositiveButton("positive") { dialog, which ->
currentNumber = 0
numberText.text = currentNumber.toString()
}

// Neutral 버튼 설정
.setNeutralButton("neutral") { dialog, which ->
Toast.makeText(requireContext(), "Number: $currentNumber", Toast.LENGTH_SHORT).show()
}

// Negative 버튼 설정
.setNegativeButton("negative") { dialog, which ->
}

// 다이얼로그 생성 및 표시
.create()
.show()
}

companion object {
fun newInstance(count: Int): CountFragment {
val fragment = CountFragment()
val args = Bundle()
args.putInt("count", count)
fragment.arguments = args
return fragment
}
}
}
34 changes: 5 additions & 29 deletions app/src/main/java/com/example/assignment02/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,42 +1,18 @@
package com.example.assignment02

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val number_text: TextView = findViewById(R.id.number_text)
val count_btn: TextView = findViewById(R.id.count_btn)
val toast_btn: TextView = findViewById(R.id.toast_btn)
val random_btn: TextView = findViewById(R.id.random_btn)
val intent = Intent(this, RandomActivity::class.java)

val resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
number_text.text = result.data?.getIntExtra("random_number", 0).toString()
Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show()
}
else {
Toast.makeText(this, "오류", Toast.LENGTH_SHORT).show()
}
}

count_btn.setOnClickListener {
number_text.text = (number_text.text.toString().toInt()+1).toString()
}
toast_btn.setOnClickListener {
Toast.makeText(this, "숫자 : "+number_text.text, Toast.LENGTH_LONG).show()
}
random_btn.setOnClickListener {
intent.putExtra("number", number_text.text.toString().toInt())
resultLauncher.launch(intent)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, CountFragment.newInstance(0))
.commit()
}
}
}
Loading