Kalkulator Sederhana
Membuat Projek
Projek dibuat dengan memilih New Project dan menggunakan Empty Activities, beri nama projek sesuai selera, saya sendiri menggunakan nama MyCalculator dengan minimum SDK API 26 Oreo. Setelah itu klik Finish.
Menyusun Sumber Kode
Aplikasi ini sangat sederhana, hanya cukup mengikuti beberapa langkah berikut:
- Buat variable num1 dan num2 untuk menyimpan nilai input dari user, jangan lupa untuk menambahkan import runtime.*
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
... import androidx.compose.runtime.* ... var num1 by remember { mutableStateOf(value = "0") } var num2 by remember { mutableStateOf(value = "0") } ... - Membuat TextField untuk menerima input dari user dengan mensingkronisasi variabel num1 dan num2
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
... TextField(value = num1, onValueChange = { num1 = it }) TextField(value = num2, onValueChange = { num2 = it }) ... - Membentuk operasi perhitungan dengan button dimana pada button menggunakan aktivitas perhitungan apabila diklik. input yang berupa string akan diubah menjadi integer dan dilakukan perhitungan yang sesuai. Hasil perhitungan akan ditampilkan sebagai pop up, hal ini dilakukan dengan Toast.
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
... Button(onClick = { var result = num1.toInt() + num2.toInt() Toast.makeText(applicationContext, "Result is $result", Toast.LENGTH_SHORT).show() }) { Text(text = "Add") } Spacer(modifier = Modifier.width(16.dp)) Button(onClick = { var result = num1.toInt() - num2.toInt() Toast.makeText(applicationContext, "Result is $result", Toast.LENGTH_SHORT).show() }) { Text(text = "Sub") } Spacer(modifier = Modifier.width(16.dp)) Button(onClick = { var result = num1.toInt() * num2.toInt() Toast.makeText(applicationContext, "Result is $result", Toast.LENGTH_SHORT).show() }) { Text(text = "Mul") } Spacer(modifier = Modifier.width(16.dp)) Button(onClick = { var result = num1.toInt() / num2.toInt() Toast.makeText(applicationContext, "Result is $result", Toast.LENGTH_SHORT).show() }) { Text(text = "Div") } ...
Hasil Akhir
Referensi: Referensi YT
Sumber kode lengkap sebagai berikut:
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
package com.example.mycalculator | |
import android.os.Bundle | |
import android.widget.Toast | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.foundation.layout.Spacer | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.height | |
import androidx.compose.foundation.layout.width | |
import androidx.compose.material3.Button | |
import androidx.compose.material3.MaterialTheme | |
import androidx.compose.material3.Surface | |
import androidx.compose.material3.Text | |
import androidx.compose.material3.TextField | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.tooling.preview.Preview | |
import com.example.mycalculator.ui.theme.MyCalculatorTheme | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.unit.dp | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
var num1 by remember { | |
mutableStateOf(value = "0") | |
} | |
var num2 by remember { | |
mutableStateOf(value = "0") | |
} | |
Column { | |
TextField(value = num1, onValueChange = { | |
num1 = it | |
}) | |
TextField(value = num2, onValueChange = { | |
num2 = it | |
}) | |
Spacer(modifier = Modifier.height(32.dp)) | |
Row { | |
Button(onClick = { | |
var result = num1.toInt() + num2.toInt() | |
Toast.makeText(applicationContext, "Result is $result", Toast.LENGTH_SHORT).show() | |
}) { | |
Text(text = "Add") | |
} | |
Spacer(modifier = Modifier.width(16.dp)) | |
Button(onClick = { | |
var result = num1.toInt() - num2.toInt() | |
Toast.makeText(applicationContext, "Result is $result", Toast.LENGTH_SHORT).show() | |
}) { | |
Text(text = "Sub") | |
} | |
Spacer(modifier = Modifier.width(16.dp)) | |
Button(onClick = { | |
var result = num1.toInt() * num2.toInt() | |
Toast.makeText(applicationContext, "Result is $result", Toast.LENGTH_SHORT).show() | |
}) { | |
Text(text = "Mul") | |
} | |
Spacer(modifier = Modifier.width(16.dp)) | |
Button(onClick = { | |
var result = num1.toInt() / num2.toInt() | |
Toast.makeText(applicationContext, "Result is $result", Toast.LENGTH_SHORT).show() | |
}) { | |
Text(text = "Div") | |
} | |
} | |
} | |
} | |
} | |
} |
Komentar
Posting Komentar