Program ini adalah program sederhana konversi satuan meter (m) ke kilometer (km) dan centimeter (cm). Saya membuat program ini menggunakan aplikasi android studio.
Pertama, saya desain terlebih dahulu tampilannya seperti berikut.
Setelah itu ketikkan script program:
activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#fff4fff5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="KONVERSI METER KE
CENTIMETER DAN KILOMETER"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Nilai Meter
(M)"
android:id="@+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="37dp"
android:layout_below="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText"
android:layout_alignBottom="@+id/textView3"
android:layout_toRightOf="@+id/textView3"
android:layout_toEndOf="@+id/textView3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centimeter
(CM)"
android:id="@+id/button"
android:layout_below="@+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="39dp"
android:onClick="klik1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kilometer (KM)"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText"
android:onClick="klik2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView2"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="140dp"
/>
</RelativeLayout>
MainActivity.java
package
titiswidiati.konversiiii;
import
android.graphics.Color;
import
android.os.Bundle;
import
android.support.v7.app.ActionBarActivity;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
public
class MainActivity extends ActionBarActivity {
Button button;
Button button2;
EditText editText;
TextView textView2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)
findViewById(R.id.button);
button2 = (Button)
findViewById(R.id.button2);
editText = (EditText) findViewById(R.id.editText);
textView2 = (TextView)
findViewById(R.id.textView2);
}
public void klik1(View view) {
try {
double m =
Integer.parseInt(editText.getText().toString());
double cm = m * 100;
textView2.setText(String.valueOf(cm));
textView2.setTextSize(30);
textView2.setTextColor(Color.RED);
} catch (Exception e) {
e.printStackTrace();
}
}
public void klik2(View view) {
try {
double m =
Integer.parseInt(editText.getText().toString());
double km = m * 0.001;
textView2.setText(String.valueOf(km));
textView2.setTextSize(30);
textView2.setTextColor(Color.BLUE);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Kalau sudah outputnya akan seperti berikut.
Meter (m) ke Centimeter (cm)
Meter (m) ke Kilometer (km)
0 komentar:
Posting Komentar