Kalkulator Android
Berikut coding .xml yang saya bikin dengan nama activity_opang.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".Opang" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/edtHitung1"
android:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edtHitung1"
android:layout_toRightOf="@+id/button1"
android:text="2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:text="4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:text="5" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:text="6" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button4"
android:text="7" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button7"
android:layout_alignBottom="@+id/button7"
android:layout_toRightOf="@+id/button7"
android:text="8" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button6"
android:layout_below="@+id/button6"
android:text="9" />
<Button
android:id="@+id/btKali"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/Kurang"
android:layout_below="@+id/btBagi"
android:layout_toRightOf="@+id/button6"
android:text="x" />
<Button
android:id="@+id/btBagi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edtHitung1"
android:layout_toRightOf="@+id/button3"
android:text="/" />
<Button
android:id="@+id/btTambah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button10"
android:layout_toLeftOf="@+id/button8"
android:text="+" />
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button8"
android:layout_alignBottom="@+id/button8"
android:layout_alignLeft="@+id/btKali"
android:text="0" />
<EditText
android:id="@+id/edtHitung1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="15dp"
android:ems="12"
android:inputType="number" />
<Button
android:id="@+id/btKurang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btTambah"
android:layout_alignBottom="@+id/btTambah"
android:layout_toLeftOf="@+id/button9"
android:layout_toRightOf="@+id/btTambah"
android:text="-" />
<Button
android:id="@+id/btHasil2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button9"
android:layout_alignRight="@+id/button10"
android:layout_below="@+id/button9"
android:text="=" />
<Button
android:id="@+id/btAwal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btKali"
android:layout_alignRight="@+id/edtHitung1"
android:text="Awal" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btTambah"
android:layout_marginTop="18dp"
android:layout_toLeftOf="@+id/btAwal"
android:text="Cara Pemakaian :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_toRightOf="@+id/btTambah"
android:text="untuk perhitungan 2 bilangan contoh 2444+22=2466" />
</RelativeLayout>
dan ini R.java yang menghubungkan dengan nama Opang_java.java
package com.UMK.opang_kalkulator;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Opang extends Activity implements OnClickListener {
double bil1=1, bil2=2,bil3=3,bil4=4,bil5=5,bil6=6,bil7=7,bil8=8,bil9=9,bil10=0;
double pd,T,Kr,Kl,B,nilaiT,nilaiKr;
Button bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt10,btT,btKr,btKl,btB,Hs,aw;
EditText Ht1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opang);
//edit text
Ht1=(EditText)findViewById(R.id.edtHitung1);
//tombol
bt1 = (Button)findViewById(R.id.button1);
bt2 = (Button)findViewById(R.id.button2);
bt3 = (Button)findViewById(R.id.button3);
bt4 = (Button)findViewById(R.id.button4);
bt5 = (Button)findViewById(R.id.button5);
bt6 = (Button)findViewById(R.id.button6);
bt7 = (Button)findViewById(R.id.button7);
bt8 = (Button)findViewById(R.id.button8);
bt9 = (Button)findViewById(R.id.button9);
bt10 = (Button)findViewById(R.id.button10);
btT = (Button)findViewById(R.id.btTambah);
btKr = (Button)findViewById(R.id.btKurang);
btKl = (Button)findViewById(R.id.btKali);
btB = (Button)findViewById(R.id.btBagi);
Hs = (Button)findViewById(R.id.btHasil2);
aw = (Button)findViewById(R.id.btAwal);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
bt5.setOnClickListener(this);
bt6.setOnClickListener(this);
bt7.setOnClickListener(this);
bt8.setOnClickListener(this);
bt9.setOnClickListener(this);
bt10.setOnClickListener(this);
btT.setOnClickListener(this);
btKr.setOnClickListener(this);
btKl.setOnClickListener(this);
btB.setOnClickListener(this);
Hs.setOnClickListener(this);
aw.setOnClickListener(this);
Ht1.setText(Double.toString(bil10));
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1 :
if((R.id.edtHitung1)>=0 | T!=0){pd=Double.parseDouble(Ht1.getText().toString());
Ht1.setText(Double.toString(bil1+(pd*10)));}
break;
case R.id.button2:
if((R.id.edtHitung1)>=0 | T!=0){pd=Double.parseDouble(Ht1.getText().toString());
Ht1.setText(Double.toString(bil2+(pd*10)));}
break;
case R.id.button3:
if((R.id.edtHitung1)>=0 | T!=0){pd=Double.parseDouble(Ht1.getText().toString());
Ht1.setText(Double.toString(bil3+(pd*10)));}
break;
case R.id.button4:
if((R.id.edtHitung1)>=0 | T!=0){pd=Double.parseDouble(Ht1.getText().toString());
Ht1.setText(Double.toString(bil4+(pd*10)));}
break;
case R.id.button5:
if((R.id.edtHitung1)>=0 | T!=0 ){pd=Double.parseDouble(Ht1.getText().toString());
Ht1.setText(Double.toString(bil5+(pd*10)));}
break;
case R.id.button6:
if((R.id.edtHitung1)>=0 | T!=0){pd=Double.parseDouble(Ht1.getText().toString());
Ht1.setText(Double.toString(bil6+(pd*10)));}
break;
case R.id.button7:
if((R.id.edtHitung1)>=0 | T!=0){pd=Double.parseDouble(Ht1.getText().toString());
Ht1.setText(Double.toString(bil7+(pd*10)));}
break;
case R.id.button8:
if((R.id.edtHitung1)>=0 | T!=0){pd=Double.parseDouble(Ht1.getText().toString());
Ht1.setText(Double.toString(bil8+(pd*10)));}
break;
case R.id.button9:
if((R.id.edtHitung1)>=0 | T!=0){pd=Double.parseDouble(Ht1.getText().toString());
Ht1.setText(Double.toString(bil9+(pd*10)));}
break;
case R.id.button10:
if((R.id.edtHitung1)>=0 | T!=0){pd=Double.parseDouble(Ht1.getText().toString());
Ht1.setText(Double.toString(bil10+(pd*10)));}
break;
case R.id.btTambah:
if((R.id.edtHitung1)>=0) {T=Double.parseDouble(Ht1.getText().toString());}
Ht1.setText(Double.toString(bil10));
break;
case R.id.btKurang:
if((R.id.edtHitung1)>=0){Kr=Double.parseDouble(Ht1.getText().toString());}
Ht1.setText(Double.toString(bil10));
break;
case R.id.btKali:
if((R.id.edtHitung1)>=0){Kl=Double.parseDouble(Ht1.getText().toString());}
Ht1.setText(Double.toString(bil10));
break;
case R.id.btBagi:
if((R.id.edtHitung1)>=0){B=Double.parseDouble(Ht1.getText().toString());}
Ht1.setText(Double.toString(bil10));
break;
case R.id.btAwal:
Ht1.setText(Double.toString(bil10));
break;
//Hasil
case R.id.btHasil2:
nilaiT=Double.parseDouble(Ht1.getText().toString());
if(T!=0) {Ht1.setText(Double.toString(T+nilaiT));}
if(Kr!=0) {Ht1.setText(Double.toString(Kr-nilaiT));}
if(Kl!=0) {Ht1.setText(Double.toString(Kl*nilaiT));}
if(B!=0) {Ht1.setText(Double.toString(B/nilaiT));}
Kr=0;
T=0;
Kl=0;
B=0;
break;
}
}
}
domain gratis http://www.000webhost.com/685133.html