๊นจ์•Œ ๊ฐœ๋… ๐Ÿ“‘/Android

[Android] Jetpack Compose - Button

interfacer_han 2024. 7. 22. 13:53

#1 ๊ฐœ์š”

 

Jetpack Compose  |  Android Developers

์ด ํŽ˜์ด์ง€๋Š” Cloud Translation API๋ฅผ ํ†ตํ•ด ๋ฒˆ์—ญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด ์ •๋ฆฌํ•˜๊ธฐ ๋‚ด ํ™˜๊ฒฝ์„ค์ •์„ ๊ธฐ์ค€์œผ๋กœ ์ฝ˜ํ…์ธ ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ถ„๋ฅ˜ํ•˜์„ธ์š”. Button ๋ฒ„ํŠผ์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜๋œ ๋ฒ„ํŠผ์„ ํŠธ๋ฆฌ๊ฑฐํ•˜๋„๋ก ํ•˜

developer.android.com

Jetpack Compose์—์„œ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณต๋˜๋Š” Composable ์ค‘ ํ•˜๋‚˜์ธ Button์— ๋Œ€ํ•ด ์‚ดํŽด๋ณธ๋‹ค.

 

#2 ์ฝ”๋“œ

#2-1 Filled Button

@Composable
fun FilledButtonExample(textParam: String, modifierParam: Modifier = Modifier) {
    val context = LocalContext.current

    Button(
        onClick = { Toast.makeText(context, "Clicked on $textParam", Toast.LENGTH_SHORT).show() },
        modifier = modifierParam
    ) {
        Text(text = textParam)
    }
}

๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฒ„ํŠผ์ด๋‹ค.

 

#2-2 Disabled Button

@Composable
fun DisabledButtonExample(textParam: String, modifierParam: Modifier = Modifier) {
    val context = LocalContext.current

    Button(
        onClick = { Toast.makeText(context, "Clicked on $textParam", Toast.LENGTH_SHORT).show() },
        modifier = modifierParam,
        enabled = false
    ) {
        Text(text = textParam)
    }
}

Button์˜ enabled ์†์„ฑ์„ false๋กœ ๋‘๋ฉด ๋ฒ„ํŠผ์ด Disabled๋œ๋‹ค.

 

#2-3 Filled Tonal Button

@Composable
fun FilledTonalButtonExample(textParam: String, modifierParam: Modifier = Modifier) {
    val context = LocalContext.current

    FilledTonalButton(
        onClick = { Toast.makeText(context, "Clicked on $textParam", Toast.LENGTH_SHORT).show() },
        modifier = modifierParam
    ) {
        Text(text = textParam)
    }
}

๋ฒ„ํŠผ์„ ์ฑ„์šฐ๋Š” ๋ฐฐ๊ฒฝ์ƒ‰(Tone)์„, ํ•ด๋‹น ๋ฒ„ํŠผ์ด ๋“ค์–ด์žˆ๋Š” Surface์— ์–ด์šธ๋ฆฌ๋Š” ์ƒ‰์œผ๋กœ ์•”์‹œ์ ์œผ๋กœ ๊ฒฐ์ •ํ•ด ๋งŒ๋“ค์–ด์ง€๋Š” ๋ฒ„ํŠผ์ด๋‹ค.

 

#2-4 Outlined Button

@Composable
fun OutlinedButtonExample(textParam: String, modifierParam: Modifier = Modifier) {
    val context = LocalContext.current

    OutlinedButton(
        onClick = { Toast.makeText(context, "Clicked on $textParam", Toast.LENGTH_SHORT).show() },
        modifier = modifierParam
    ) {
        Text(text = textParam)
    }
}

๋ฒ„ํŠผ์˜ ๋ฐฐ๊ฒฝ์ƒ‰์ด ์ฑ„์›Œ์ง€์ง€ ์•Š๊ณ  ํ…Œ๋‘๋ฆฌ๋งŒ์œผ๋กœ ๊ทธ ์˜์—ญ์„ ๋“œ๋Ÿฌ๋‚ด๋Š” ๋ฒ„ํŠผ์ด๋‹ค.

 

#2-5 Elevated Button

@Composable
fun ElevatedButtonExample(textParam: String, modifierParam: Modifier = Modifier) {
    val context = LocalContext.current

    ElevatedButton(
        onClick = { Toast.makeText(context, "Clicked on $textParam", Toast.LENGTH_SHORT).show() },
        modifier = modifierParam
    ) {
        Text(text = textParam)
    }
}

๋ฒ„ํŠผ์— ๋†’์ด๊ฐ€ ์žˆ๋Š” ๊ฒƒ๊ฐ™์€ ํšจ๊ณผ๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค. 

 

#2-6 Text Button

@Composable
fun TextButtonExample1(textParam: String, modifierParam: Modifier = Modifier) {
    val context = LocalContext.current

    TextButton(
        onClick = { Toast.makeText(context, "Clicked on $textParam", Toast.LENGTH_SHORT).show() },
        modifier = modifierParam
    ) {
        Text(text = textParam)
    }
}

@Composable
fun TextButtonExample2(textParam: String, modifierParam: Modifier = Modifier) {
    val context = LocalContext.current

    Text(
        modifier = modifierParam.clickable { Toast.makeText(context, "Clicked on $textParam", Toast.LENGTH_SHORT).show() },
        text = textParam
    )
}

Text์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ Button์ธ ๊ฒฝ์šฐ๋‹ค. ๊ทธ๋ƒฅ Text์—๋„ Modifier์— clickable() ๋ฉ”์†Œ๋“œ๋ฅผ ์ ์šฉํ•ด ๋ฒ„ํŠผ์ฒ˜๋Ÿผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค. ๋ฌผ๋ก  ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์˜ˆ์˜๊ฒŒ ๋‹ค๋“ฌ์ง€ ์•Š์€, ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต๋˜๋Š” ๋ชจ์–‘์ƒˆ๋งŒ ๋ดค์„ ๋•Œ๋Š” TextButton์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฏธ๊ด€ ์ƒ ๋” ์ข‹๋‹ค.

 

#2-7 Icon Button

@Composable
fun IconButtonExample(imageVectorParam: ImageVector, modifierParam: Modifier = Modifier) {
    val context = LocalContext.current

    IconButton(
        onClick = { Toast.makeText(context, "Clicked on ${imageVectorParam.name}", Toast.LENGTH_SHORT).show() },
        modifier = modifierParam
    ) {
        Icon(
            imageVectorParam,
            contentDescription = imageVectorParam.name,
            tint = Color.DarkGray,
            modifier = Modifier.size(40.dp)
        )
    }
}

๋ง ๊ทธ๋Œ€๋กœ ํด๋ฆญ ๊ฐ€๋Šฅํ•œ ์•„์ด์ฝ˜์ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. ์ด ๋ฒ„ํŠผ Composable์€ Icon์„ ์ž์‹ ์•„์ดํ…œ์œผ๋กœ ๊ฐ–๋Š”๋‹ค. imageVectorParam์œผ๋กœ๋Š” Icons.Filled.Refresh์™€ ๊ฐ™์ด ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณต๋˜๋Š” ์•„์ด์ฝ˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

#3 ์ž‘๋™ ํ™•์ธ

#2์˜ ๋ชจ๋“  ๋ฒ„ํŠผ์„ Column์— ๋„ฃ๊ณ  ์•ˆ๋“œ๋กœ์ด๋“œ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹คํ–‰์‹œํ‚จ ๋ชจ์Šต์ด๋‹ค.

 

#4 ์™„์„ฑ๋œ ์•ฑ

 

android-practice/jetpack-compose/Button at master ยท Kanmanemone/android-practice

Contribute to Kanmanemone/android-practice development by creating an account on GitHub.

github.com