This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
DefaultTheme { | |
// A surface container using the 'background' color from the theme | |
Surface( | |
modifier = Modifier.fillMaxSize(), | |
color = MaterialTheme.colorScheme.background | |
) { | |
CalculatorApp() | |
} | |
} | |
} | |
} | |
} | |
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") | |
@OptIn(ExperimentalMaterial3Api::class) | |
@Composable | |
fun CalculatorApp() { | |
var num1 by remember { mutableStateOf("") } | |
var num2 by remember { mutableStateOf("") } | |
var result by remember { mutableStateOf("") } | |
Scaffold( | |
topBar = { | |
TopAppBar( | |
title = { | |
Text(text = "계산기 만들어봐용") | |
} | |
) | |
}, | |
content = { | |
Column( | |
modifier = Modifier.fillMaxSize(), | |
verticalArrangement = Arrangement.Center, | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
TextField( | |
value = num1, | |
onValueChange = { num1 = it }, | |
label = { Text(text = "숫자 1") }, | |
modifier = Modifier.padding(16.dp) | |
) | |
TextField( | |
value = num2, | |
onValueChange = { num2 = it }, | |
label = { Text(text = "숫자 2") }, | |
modifier = Modifier.padding(16.dp) | |
) | |
Spacer(modifier = Modifier.height(16.dp)) | |
Button( | |
onClick = { | |
val n1 = num1.toIntOrNull() | |
val n2 = num2.toIntOrNull() | |
if (n1 != null && n2 != null) { | |
result = (n1 + n2).toString() | |
} else { | |
result = "Invalid input" | |
} | |
}, | |
modifier = Modifier.padding(16.dp) | |
) { | |
Text(text = "더하기") | |
} | |
Button( | |
onClick = { | |
val n1 = num1.toIntOrNull() | |
val n2 = num2.toIntOrNull() | |
if (n1 != null && n2 != null) { | |
result = (n1 - n2).toString() | |
} else { | |
result = "Invalid input" | |
} | |
}, | |
modifier = Modifier.padding(16.dp) | |
) { | |
Text(text = "빼기") | |
} | |
Button( | |
onClick = { | |
val n1 = num1.toIntOrNull() | |
val n2 = num2.toIntOrNull() | |
if (n1 != null && n2 != null) { | |
result = (n1 * n2).toString() | |
} else { | |
result = "Invalid input" | |
} | |
}, | |
modifier = Modifier.padding(16.dp) | |
) { | |
Text(text = "곱하기") | |
} | |
Button( | |
onClick = { | |
val n1 = num1.toIntOrNull() | |
val n2 = num2.toIntOrNull() | |
if (n1 != null && n2 != null) { | |
result = (n1 / n2).toString() | |
} else { | |
result = "Invalid input" | |
} | |
}, | |
modifier = Modifier.padding(16.dp) | |
) { | |
Text(text = "나누기") | |
} | |
Text( | |
text = "결과: $result", | |
style = MaterialTheme.typography.labelLarge | |
) | |
} | |
} | |
) | |
} | |
@Preview(showBackground = true) | |
@Composable | |
fun GreetingPreview() { | |
DefaultTheme { | |
CalculatorApp() | |
} | |
} |

'Compose' 카테고리의 다른 글
LazyVerticalGrid (0) | 2023.05.11 |
---|---|
compose 계산기 수정 (0) | 2023.05.10 |
navigation (0) | 2023.05.07 |
lazyColumn (0) | 2023.05.07 |
scaffold (0) | 2023.05.07 |