Membuat Kalkulator Android di ECLIPSE
1. Buatlah Project Baru dengan nama "MyCalculator"
2. Setelah selesai membuat projek baru, tambahkan kodingan berikut pada activity_main.xml, kemudian Save:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/teks1"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:hint="Masukan Angka
Pertama">
</EditText>
<EditText
android:id="@+id/teks2"
android:layout_height="70dp"
android:hint="Masukan Angka
Kedua"
android:layout_width="fill_parent">
</EditText>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/tbl3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Tambah"
android:text="Tambah"
>
</Button>
<Button
android:id="@+id/tbl1"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:onClick="Kurang"
android:text="Kurang"
>
</Button>
<Button
android:id="@+id/tbl2"
android:layout_height="wrap_content"
android:text="Kali"
android:onClick="Kali"
android:layout_width="80dp">
</Button>
<Button
android:id="@+id/tbl4"
android:layout_height="wrap_content"
android:onClick="Bagi"
android:text="Bagi"
android:layout_width="80dp">
</Button>
</LinearLayout>
<EditText
android:id="@+id/teks3"
android:layout_height="70dp"
android:hint="Hasil"
android:layout_width="fill_parent">
</EditText>
</LinearLayout>
package
com.example.mycalculator;
import
android.app.Activity;
import
android.os.Bundle;
import
android.widget.*;
import
android.view.*;
public class
MainActivity extends Activity {
/** Called when the
activity is first created. */
EditText one,two,hasil;
Button tambah,kurang,kali,bagi;
double satu,dua,hsl;
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one=(EditText)
findViewById(R.id.teks1);
two=(EditText)
findViewById(R.id.teks2);
hasil=(EditText)
findViewById(R.id.teks3);
tambah=(Button)
findViewById(R.id.tbl1);
kurang=(Button)
findViewById(R.id.tbl2);
kali=(Button)
findViewById(R.id.tbl3);
bagi=(Button)
findViewById(R.id.tbl4);
}
public void
Tambah(View view){
satu=Double.parseDouble(one.getText().toString());
dua=Double.parseDouble(two.getText().toString());
hsl=satu+dua;
hasil.setText(Double.toString(hsl));
}
public void Kurang(View view){
satu=Double.parseDouble(one.getText().toString());
dua=Double.parseDouble(two.getText().toString());
hsl=satu-dua;
hasil.setText(Double.toString(hsl));
}
public void
Kali(View view){
satu=Double.parseDouble(one.getText().toString());
dua=Double.parseDouble(two.getText().toString());
hsl=satu*dua;
hasil.setText(Double.toString(hsl));
}
public void
Bagi(View view){
satu=Double.parseDouble(one.getText().toString());
dua=Double.parseDouble(two.getText().toString());
hsl=satu/dua;
hasil.setText(Double.toString(hsl));
}
}
Berikut Tampilan akhirnya...
Penting!
Pada package com.example.mycalculator; milik anda jangan dihapus
Download Aplikasi Tutorial Android lainnya di bawah ini :
Via Google Drive |
0 Response to "Membuat Kalkulator Android di ECLIPSE"
Post a Comment