Skip to content
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

[Section 1] compose basic #1

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

1 change: 0 additions & 1 deletion .idea/misc.xml

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

10 changes: 10 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ dependencies {
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
implementation ("androidx.compose.material3:material3:1.1.2")

// Navigation
implementation("androidx.navigation:navigation-compose:2.7.5")

// ViewModel
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2")

// LiveData
implementation("androidx.compose.runtime:runtime-livedata:1.5.4")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,59 @@ package com.example.modernprograming_jetpackcompose
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.Surface
import com.example.modernprograming_jetpackcompose.ui.theme.ModernPrograming_JetpackComposeTheme
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ModernPrograming_JetpackComposeTheme {
Surface() {

}
}
HomeScreen()
}
}
}
}

@Composable
fun HomeScreen(viewModel: MainViewModel = viewModel()) {
val text3: State<String> = viewModel.liveData.observeAsState("")

Column {
Text(text = text3.value)
TextField(value = text3.value, onValueChange = { viewModel.changeValue(it) })
// Button(onClick = {
// viewModel.changeValue(text3.value)
// }) {
// Text(text = "클릭")
// }
}
}

class MainViewModel : ViewModel() {
private val _value = mutableStateOf("Hello World")
val value: State<String> = _value

// LiveData 사용하기
private val _liveData = MutableLiveData<String>()
val liveData: LiveData<String> get() = _liveData

fun changeValue(value: String) {
_liveData.value = value
}
}


@Preview(showBackground = true)
@Composable
fun DefaultPreview() {

}
Binary file added app/src/main/res/drawable/img_my_melody.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.