Вы находитесь на странице: 1из 6

PERT 2 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" >

<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Masukkan suhu" />

<EditText
android:id="@+id/editText1"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal" >
<requestFocus />

</EditText>
<Spinner android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Konversi suhu"
android:onClick="Konversi"/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Celcius" />

<EditText
android:id="@+id/editText2"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="number"
android:editable="false" />

</TableRow>
<TableRow>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reamur" />

<EditText
android:id="@+id/editText3"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="number" android:editable="false"/>

</TableRow>
<TableRow>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fahrenheit" />

<EditText
android:id="@+id/editText4"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="number"
android:editable="false" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kelvin" />
<EditText
android:id="@+id/editText5"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="number"
android:editable="false" />
</TableRow>
</TableLayout>
</RelativeLayout>

Pert 2 java
package com.example.hafidz_2016140082;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class Pertemuan_2 extends Activity {


private Spinner sp;
private EditText edt_awal, edt_C, edt_R, edt_F, edt_K;
private String[] list={"C","R","F","K"};
Double awal, celcius, reamur, fahrenheit, kelvin;
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pertemuan_2);
sp=(Spinner) findViewById(R.id.spinner1);
edt_awal=(EditText) findViewById(R.id.editText1);
edt_C=(EditText) findViewById(R.id.editText2);
edt_R=(EditText) findViewById(R.id.editText3);
edt_F=(EditText) findViewById(R.id.editText4);
edt_K=(EditText) findViewById(R.id.editText5);

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);


adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
}

public void Konversi(View v){


String satuan = String.valueOf(sp.getSelectedItemPosition());
if(edt_awal.getText().toString().equals("")){
Toast.makeText(getBaseContext(), "Masukkan suhu awal, default suhu awal = 0 jika
tidak dimasukkan suhu awal", Toast.LENGTH_LONG).show(); awal = 0.0;
}
else{ awal = Double.parseDouble(edt_awal.getText().toString()); }
if(satuan.equals("0")){ celcius = awal;
reamur = 0.8 * awal;
fahrenheit = (1.8 * awal) + 32;
kelvin = awal + 273; edt_C.setText(String.valueOf(celcius));
edt_R.setText(String.valueOf(reamur));
edt_F.setText(String.valueOf(fahrenheit));
edt_K.setText(String.valueOf(kelvin));
}
else if(satuan.equals("1")){
celcius = 1.25 * awal;
reamur = awal;
fahrenheit = (2.25 * awal) + 32; kelvin = celcius + 273;
edt_C.setText(String.valueOf(celcius));
edt_R.setText(String.valueOf(reamur));
edt_F.setText(String.valueOf(fahrenheit));
edt_K.setText(String.valueOf(kelvin));
}
else if(satuan.equals("2")){
celcius = 0.55555 *(awal - 32);
reamur = 0.44444 * (awal-32);
fahrenheit = awal; kelvin = celcius + 273;
edt_C.setText(String.valueOf(celcius));
edt_R.setText(String.valueOf(reamur));
edt_F.setText(String.valueOf(fahrenheit));
edt_K.setText(String.valueOf(kelvin));
}
else if(satuan.equals("3")){
celcius = awal-273;
reamur = 0.8 * (awal-273);
fahrenheit = (1.8 * (awal-273)) + 32;
kelvin = awal; edt_C.setText(String.valueOf(celcius));
edt_R.setText(String.valueOf(reamur));
edt_F.setText(String.valueOf(fahrenheit));
edt_K.setText(String.valueOf(kelvin));
}
}
}

Вам также может понравиться