์ง๋ ํฌ์คํ
https://rkdrkd-history.tistory.com/41
์ด๋ฒ ํฌ์คํ ์์๋ ํ์๊ฐ xml์ ์ฌ์ฉํ ๋ ์์ฃผ ์ฌ์ฉํ๋ ๋ทฐ๋ค์ compose๋ก ์์ฑํด๋ณด๊ฒ ๋ค.
2. ๋ค์ํ ๋ทฐ ์์ฑ
1. edit text
TextField๋ฅผ ์ฌ์ฉํด ํ ์คํธ ์ ๋ ฅ ์นธ์ ๊ตฌํํ ์ ์๋ค.
@Composable
fun Greeting2(name: String) {
Text(text = name)
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview2() {
ComposeUITheme {
Box(modifier = Modifier
.fillMaxWidth()
.height(120.dp)) {
Column(modifier = Modifier.padding(15.dp)) {
Greeting2("Hello")
val text = remember { mutableStateOf("") }
TextField(value = text.value, onValueChange = { textValue -> text.value = textValue })
}
}
}
}
๋ณด๊ธฐ ํธํ๊ฒ Box ์์์ ์์ ์ ํ๊ณ , padding๋ 15dp๋งํผ ์ฃผ์๋ค.
์ค์ ํ ํ ์คํธ์ TextField๊ฐ ์ ๋ํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
์คํ ํ๋ฉด
2. imageView
์ด๋ฏธ์ง ๋ทฐ๋ฅผ ๊ทธ๋ฆฌ๋ ๋ฐฉ๋ฒ์ Image๋ฅผ ์ฌ์ฉํด์ ๊ทธ๋ฆด ์ ์๋ค.
@Composable
fun ImageView() {
Image(painter = painterResource(id = R.drawable.ic_launcher_background),
contentDescription = null // ํ์ ์์
)
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview2() {
ComposeUITheme {
Box(modifier = Modifier
.fillMaxWidth()
.height(120.dp)
.padding(15.dp)) {
ImageView()
}
}
}
image ์์ contentDescription์ ์ฐ์ง ์์ผ๋ฉด ์ค๋ฅ๊ฐ ๋๊ธฐ ๋๋ฌธ์ ๊ผญ ์จ๋๋๋ก ํ์.
์คํ ํ๋ฉด
์ด๋ฏธ์ง์ ํฌ๊ธฐ ์กฐ์ ๋ฐฉ๋ฒ
@Composable
fun ImageView() {
Image(painter = painterResource(id = R.drawable.ic_launcher_background),
contentDescription = null,
modifier = Modifier.width(150.dp).height(150.dp)
)
}
modifier ์์ width์ height๋ฅผ ๊ฐ๊ฐ ์ ๋ ฅํด์ฃผ์๋ค.
์คํ ๊ฒฐ๊ณผ
์์ 1
๋ก๊ทธ์ธ ํ์ด์ง ๋ง๋ค๊ธฐ
@Preview(showBackground = true)
@Composable
fun DefaultPreview2() {
ComposeUITheme {
Box(modifier = Modifier
.fillMaxWidth()
.fillMaxHeight()) {
Column(modifier = Modifier.align(Alignment.Center)) {
Image(painter = painterResource(id = R.drawable.practice_example_1),
contentDescription = null,
modifier = Modifier
.width(150.dp)
.height(150.dp)
.align(Alignment.CenterHorizontally)
.offset(y = (-20).dp))
val text = remember { mutableStateOf("") }
val password = remember { mutableStateOf("") }
// ๋น๋ฐ๋ฒํธ ๋ณด์ด๊ฒ ํ๋ ์ฌ๋ถ
var passwordVisibility by remember { mutableStateOf(false) }
val icon = if (passwordVisibility) {
painterResource(id = R.drawable.ic_baseline_visibility_24)
} else painterResource(id = R.drawable.ic_baseline_visibility_off_24)
// ์ด๋ฉ์ผ
TextField(value = text.value, onValueChange = { textValue -> text.value = textValue },
label = {Text(text = "email")}, modifier = Modifier.offset(y = (-10).dp))
// ๋น๋ฐ๋ฒํธ
TextField(value = password.value, onValueChange = { textValue -> password.value = textValue },
label = {Text(text = "password")},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
trailingIcon = {
IconButton(onClick = {
passwordVisibility = !passwordVisibility
}) {
Icon(painter = icon, contentDescription = null)
}
},
visualTransformation = if (passwordVisibility) VisualTransformation.None
else PasswordVisualTransformation()
)
}
}
}
}
TextField ์์ฑ
- keyboardOptions: ํค๋ณด๋์ ์์ฑ์ ์ ์ํ๋ค.(๋น๋ฐ๋ฒํธ, ์ซ์, ์ด๋ฉ์ผ ๋ฑ๋ฑ)
- trailingIcon: ํ ์คํธ๋ฅผ visibility๋ฅผ ์ค์ ํ๋ icon์ ์ค์ ํ๊ฒ ํด ์ค๋ค.
- visualTransformation: ํ ์คํธ์ visibility์ ์ค์ ํ๋ค.
text์ password์ mutableState๋ฅผ ๋ฐ๋ก ์ค์ ํ ์ด์ ๋ํ๋๋ง ์ฌ์ฉํ๋ฉด 1๊ฐ์ TextField์ ํ ์คํธ๋ฅผ ์ ๋ ฅํ ๋ ๋ค๋ฅธ TextField์ ๊ฐ์ด ์๋์ผ๋ก ์ค์ ์ค์ธ ๊ฐ์ผ๋ก ๋ฐ๋๊ธฐ ๋๋ฌธ์ด๋ค.
์คํ ๊ฒฐ๊ณผ
ํ ์คํธ visibility๊ฐ ์ ์ค์ ๋์๋ค.
'๐ฑ| Android > ๐ | Jetpack' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android, Kotlin] Compose UI ์ ๋ฆฌ(4) (0) | 2022.12.05 |
---|---|
[Android, Kotlin] Compose UI ์ ๋ฆฌ(3) (0) | 2022.12.04 |
[Android, Kotlin] Compose UI ์ ๋ฆฌ(1) (0) | 2022.12.01 |
[Android Kotlin] navigation component์ bottom navigation ์ฐ๊ฒฐ ํ์๋, bottom navigation์ด ์๋๋์ง ์์๋ ํด๊ฒฐ ๋ฐฉ๋ฒ (0) | 2022.04.22 |
[Android] Room ์ฌ์ฉ๋ฒ๊ณผ RecyclerView๋ฅผ ํ์ฉํ ์์ (0) | 2022.04.12 |