CARA MEMBUAT KALKULATOR SEDERHANA DENGAN ANDROID STUDIO
Halo, kali ini saya akan membahas sedikit tentang penggunaan Android Studio dengan membuat aplikasi kalkulator sederhana.
Hal pertama yang harus kita lakukan yaitu membuat layout kalkulatornya terlebih dahulu. Buka app >> res >> layout >> activity_main.xml. Buat layout sesuai dengan gambar di bawah. Caranya hanya tinggal drag komponen yang kita butuhkan. Seperti button dan text view.
Kemudian sesuaikan ID pada text di activity_main.xml. Kemudian buat code perintah pada java aplikasi kita, seperti berikut.
Pertama-tama, kita deklarasikan semua komponen dan fungsi layout yang kita gunakan menggunakan code seperti berikut.
package com.example.fathur.kalkulatorfathur; import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView; public class MainActivity extends AppCompatActivity { private Button zero;
private Button one;
private Button two;
private Button three;
private Button four; private Button five; private Button six; private Button seven; private Button eight; private Button nine; private Button add; private Button sub; private Button mul; private Button div; private Button equal; private Button clear; private TextView info; private TextView result; private final char ADDITION = '+'; private final char SUBSTRACTION = '-'; private final char MULTIPLY = '*'; private final char DIVISION = '/'; private final char EQU = 0; private double val1 = Double.NaN; private double val2; private char ACTION; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setupUIViews(); zero.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { info.setText(info.getText().toString() + "0"); } }); one.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { info.setText(info.getText().toString() + "1"); } }); two.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { info.setText(info.getText().toString() + "2"); } }); three.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { info.setText(info.getText().toString() + "3"); } }); four.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { info.setText(info.getText().toString() + "4"); } }); five.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { info.setText(info.getText().toString() + "5"); } }); six.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { info.setText(info.getText().toString() + "6"); } }); seven.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { info.setText(info.getText().toString() + "7"); } }); eight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { info.setText(info.getText().toString() + "8"); } }); nine.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { info.setText(info.getText().toString() + "9"); } }); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { compute(); ACTION = ADDITION; result.setText(String.valueOf(val1) + "+"); info.setText(null); } }); sub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { compute(); ACTION = SUBSTRACTION; result.setText(String.valueOf(val1) + "-"); info.setText(null); } }); mul.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { compute(); ACTION = MULTIPLY; result.setText(String.valueOf(val1) + "*"); info.setText(null); } }); div.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { compute(); ACTION = DIVISION; result.setText(String.valueOf(val1) + "/"); info.setText(null); } }); equal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { compute(); ACTION = EQU; result.setText(result.getText().toString() + String.valueOf(val2) + "=" + String.valueOf(val1)); info.setText(null); } }); clear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(info.getText().length() > 0){ CharSequence name = info.getText().toString(); info.setText(name.subSequence(0, name.length()-1)); } else{ val1 = Double.NaN; val2 = Double.NaN; info.setText(null); result.setText(null); } } }); }
private void setupUIViews(){ zero = (Button)findViewById(R.id.btn0); one = (Button)findViewById(R.id.btn1); two = (Button)findViewById(R.id.btn2); three = (Button)findViewById(R.id.btn3); four = (Button)findViewById(R.id.btn4); five = (Button)findViewById(R.id.btn5); six = (Button)findViewById(R.id.btn6); seven = (Button)findViewById(R.id.btn7); eight = (Button)findViewById(R.id.btn8); nine = (Button)findViewById(R.id.btn9); add = (Button)findViewById(R.id.btnadd); sub = (Button)findViewById(R.id.btnsub); mul = (Button)findViewById(R.id.btnmul); div = (Button)findViewById(R.id.btndiv); equal = (Button)findViewById(R.id.btnequal); clear = (Button)findViewById(R.id.btnclr); info = (TextView)findViewById(R.id.tvctrl); result = (TextView)findViewById(R.id.tvrslt);}
Lalu, buatlah code untuk menjalankan komponen layout yang telah kita buat dengan menggunakan code sebagai berikut.
private void compute(){ if(!Double.isNaN(val1)){ val2 = Double.parseDouble(info.getText().toString()); switch(ACTION){ case ADDITION: val1 = val1 + val2; break; case SUBSTRACTION: val1 = val1 - val2; break; case MULTIPLY: val1 = val1 * val2; break; case DIVISION: val1 = val1 / val2; break; case EQU: break; } } else{ val1 = Double.parseDouble(info.getText().toString()); } } }
Seperti itulah penjelasan singkat mengenai cara membuat aplikasi kalkulator sederhana dengan menggunakan Android Studio. Semoga bermanfaat!
Comments
Post a Comment