Sebelum kita masuk pada langkah-langkah pembuatan, kita harus tau dulu apa rumus untuk mencari luas persegi panjang terlebih dahulu karena didalam program nanti kita akan mendeklarasikan rumus dari persegi panjang itu sendriri.
Langkah awal yang kita lakukan yaitu jalankan aplikasi ADT, buat project dengan nama Perhitungan_luas.
Langkah kedua buat kelas dengan nama Hitung_luas.java dan file xml dengan nama Hitung_luas.java. setelah itu masukkan source code pada file Hitung_luas.java
package com.example.Perhitungan_luas;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Hitung_luas extends Activity implements OnClickListener {
Button but1;
Button but2;
Button but3;
Button but4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Hitung_luas);
but1 = (Button) findViewById(R.id.button1);
but2 = (Button) findViewById(R.id.button2);
but3 = (Button) findViewById(R.id.button3);
but4 = (Button) findViewById(R.id.button4);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent(Hitung_luas.this,Tugas1.class);
startActivity(masuk);
}
});
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent(Hitung_luas.this,Tugas2.class);
startActivity(masuk);
}
});
but3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent(Hitung_luas.this,Tugas3.class);
startActivity(masuk);
}
});
but4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent(Hitung_luas.this,Tugas4.class);
startActivity(masuk);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.Hitung_luas, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Hitung_luas extends Activity implements OnClickListener {
Button but1;
Button but2;
Button but3;
Button but4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Hitung_luas);
but1 = (Button) findViewById(R.id.button1);
but2 = (Button) findViewById(R.id.button2);
but3 = (Button) findViewById(R.id.button3);
but4 = (Button) findViewById(R.id.button4);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent(Hitung_luas.this,Tugas1.class);
startActivity(masuk);
}
});
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent(Hitung_luas.this,Tugas2.class);
startActivity(masuk);
}
});
but3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent(Hitung_luas.this,Tugas3.class);
startActivity(masuk);
}
});
but4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent masuk = new Intent(Hitung_luas.this,Tugas4.class);
startActivity(masuk);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.Hitung_luas, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
Langkah ketiga masukkan source code untuk file Hitung_luas.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Pindah" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="Layout Hitung Luas"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="48dp"
android:layout_marginTop="58dp"
android:text="Tugas 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="42dp"
android:layout_toRightOf="@+id/button1"
android:text="Tugas 2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="46dp"
android:text="Tugas 3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button3"
android:layout_alignLeft="@+id/button2"
android:text="Tugas 4" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Pindah" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="Layout Hitung Luas"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="48dp"
android:layout_marginTop="58dp"
android:text="Tugas 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="42dp"
android:layout_toRightOf="@+id/button1"
android:text="Tugas 2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="46dp"
android:text="Tugas 3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button3"
android:layout_alignLeft="@+id/button2"
android:text="Tugas 4" />
</RelativeLayout>
Langkah keempat buat lagi kelas dengan nama Tugas1.java dan file xml Tugas1.xml. kemudian masukkan source code pada file Tugas1.java
package com.example.Perhitungan_luas;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Tugas1 extends Activity {
Button satu;
EditText editText1;
EditText editText2;
EditText editText3;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tugas1);
satu = (Button) findViewById(R.id.satu);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
button1 = (Button) findViewById(R.id.button1);
satu.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent pindah = new Intent(Tugas1.this,Hitung_luas.class);
startActivity(pindah);
}
});
}
public void hitungluas(View View) {
try {
int panjang = Integer.parseInt(editText1.getText().toString());
int lebar = Integer.parseInt(editText2.getText().toString());
int luas = panjang * lebar;
editText3.setText(String.valueOf(luas));
} catch (Exception e) {
e.printStackTrace();
}
}
}
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Tugas1 extends Activity {
Button satu;
EditText editText1;
EditText editText2;
EditText editText3;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tugas1);
satu = (Button) findViewById(R.id.satu);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
button1 = (Button) findViewById(R.id.button1);
satu.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent pindah = new Intent(Tugas1.this,Hitung_luas.class);
startActivity(pindah);
}
});
}
public void hitungluas(View View) {
try {
int panjang = Integer.parseInt(editText1.getText().toString());
int lebar = Integer.parseInt(editText2.getText().toString());
int luas = panjang * lebar;
editText3.setText(String.valueOf(luas));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Langkah kelima masukkan lagi source code pada file Tugas1.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Satu" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignParentTop="true"
android:text="@string/panjang"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView3"
android:ems="10"
android:inputType="numberSigned|numberDecimal" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText1"
android:text="@string/lebar"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/satu"
android:layout_below="@+id/textView2"
android:ems="10"
android:inputType="numberSigned|numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText2"
android:onClick="hitungluas"
android:text="@string/hitungluas" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="@string/luas"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="numberSigned|numberDecimal" />
<Button
android:id="@+id/satu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:text="@string/back" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Satu" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignParentTop="true"
android:text="@string/panjang"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView3"
android:ems="10"
android:inputType="numberSigned|numberDecimal" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText1"
android:text="@string/lebar"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/satu"
android:layout_below="@+id/textView2"
android:ems="10"
android:inputType="numberSigned|numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText2"
android:onClick="hitungluas"
android:text="@string/hitungluas" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="@string/luas"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="numberSigned|numberDecimal" />
<Button
android:id="@+id/satu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:text="@string/back" />
</RelativeLayout>
Tampilan yang dihasilkan pada emulator:
0 komentar:
Posting Komentar