widiarta's site | Kalkulator Sederhana Copyright Made Widiarta
[email protected] http://widiarta.staff.ipb.ac.id/2011/02/21/kalkulator-sederhana/
Kalkulator Sederhana Tutorial berikut akan mengajarkan rekan-rekan cara membuat kalkulator sederhana di android.
pertama-tama pastikan di komputer anda telah terinsal AVD dan eclipse.
1. Buat projek android baru dengan nama kalkulatorku
2. Buat AVD dengan menggunakan platform Android 2.1
3. Pada bagian kalkulatorku.java tuliskan kode program berikut:
package com.kalkulatorku;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
page 1 / 10
widiarta's site | Kalkulator Sederhana Copyright Made Widiarta
[email protected] http://widiarta.staff.ipb.ac.id/2011/02/21/kalkulator-sederhana/
import android.view.View;
public class kalkulatorku extends Activity
{
private EditText txtbillamount;
private EditText txtpeople;
private EditText txtpercentage;
private TextView txtperperson;
private TextView txttipamount;
private TextView txttotal;
private Button btncalculate;
private Button btnreset;
private double billamount = 0;
page 2 / 10
widiarta's site | Kalkulator Sederhana Copyright Made Widiarta
[email protected] http://widiarta.staff.ipb.ac.id/2011/02/21/kalkulator-sederhana/
private double percentage = 0;
private double numofpeople=0;
private double tipamount = 0;
private double totaltopay = 0;
private double perperson = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
page 3 / 10
widiarta's site | Kalkulator Sederhana Copyright Made Widiarta
[email protected] http://widiarta.staff.ipb.ac.id/2011/02/21/kalkulator-sederhana/
}
private void initControls()
{
txtbillamount = (EditText)findViewById(R.id.txtbillamount);
txtpeople = (EditText)findViewById(R.id.txtpeople);
txtpercentage = (EditText)findViewById(R.id.txtpercentage);
txtperperson=(TextView)findViewById(R.id.txtperperson);
txttipamount=(TextView)findViewById(R.id.txttipamount);
txttotal=(TextView)findViewById(R.id.txttotal);
btncalculate = (Button)findViewById(R.id.btncalculate);
btnreset = (Button)findViewById(R.id.btnreset);
btncalculate.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }});
page 4 / 10
widiarta's site | Kalkulator Sederhana Copyright Made Widiarta
[email protected] http://widiarta.staff.ipb.ac.id/2011/02/21/kalkulator-sederhana/
btnreset.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ reset(); }});
}
private void calculate()
{
billamount=Double.parseDouble(txtbillamount.getText().toString());
percentage=Double.parseDouble(txtpercentage.getText().toString());
numofpeople=Double.parseDouble(txtpeople.getText().toString());
tipamount=(billamount*percentage)/100;
totaltopay=billamount+tipamount;
perperson=totaltopay/numofpeople;
txttipamount.setText(Double.toString(tipamount));
txttotal.setText(Double.toString(totaltopay));
page 5 / 10
widiarta's site | Kalkulator Sederhana Copyright Made Widiarta
[email protected] http://widiarta.staff.ipb.ac.id/2011/02/21/kalkulator-sederhana/
txtperperson.setText(Double.toString(perperson));
}
private void reset()
{
txtbillamount.setText("");
txtpeople.setText("");
txtpercentage.setText("");
txtperperson.setText("");
txttipamount.setText("");
txttotal.setText("");
} }
4. Pada bagian main.xml silahkan ketik kode program berikut :
page 6 / 10
widiarta's site | Kalkulator Sederhana Copyright Made Widiarta
[email protected] http://widiarta.staff.ipb.ac.id/2011/02/21/kalkulator-sederhana/
page 7 / 10
widiarta's site | Kalkulator Sederhana Copyright Made Widiarta [email protected] http://widiarta.staff.ipb.ac.id/2011/02/21/kalkulator-sederhana/
> <Button android:id="@+id/btncalculate" android:layout_width="87px" android:layout_height="wrap_content" android:text="Calculate" android:layout_x="40px" android:layout_y="182px" > <Button android:id="@+id/btnreset" android:layout_width="86px" android:layout_height="wrap_content" android:text="Reset" android:layout_x="140px" android:layout_y="182px" > <EditText android:id="@+id/txtbillamount" android:layout_width="wrap_content" android:layout_height="35px" android:text="100" android:textSize="18sp" android:layout_x="200px" android:layout_y="22px"
page 8 / 10
widiarta's site | Kalkulator Sederhana Copyright Made Widiarta [email protected] http://widiarta.staff.ipb.ac.id/2011/02/21/kalkulator-sederhana/
> <EditText android:id="@+id/txtpercentage" android:layout_width="51px" android:layout_height="36px" android:text="10" android:textSize="18sp" android:layout_x="200px" android:layout_y="72px" > <EditText android:id="@+id/txtpeople" android:layout_width="wrap_content" android:layout_height="39px" android:text="1" android:textSize="18sp" android:layout_x="200px" android:layout_y="122px" >
page 9 / 10
widiarta's site | Kalkulator Sederhana Copyright Made Widiarta [email protected] http://widiarta.staff.ipb.ac.id/2011/02/21/kalkulator-sederhana/
android:layout_y="352px" >
5. Jalankan program anda.
=========== SELAMAT MENCOBA ===========
sumber : http://www.datasprings.com
page 10 / 10