Convertitore di temperatura

Creazione di una applicazione capace di convertire le temperature da °C a °F e viceversa

 

-----------------ConvertiTemperatura.java-----------------

package org.me.convertitemperatura;

import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog.Builder;
import android.widget.*;
import android.view.View;
import android.content.Intent;
import android.app.AlertDialog;
import android.content.Context;
/**
*
* @author lollino360
*/
public class ConvertiTemperatura extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        // ToDo add your GUI initialization code here
    }
public void conv1(View view){
            EditText numero1=(EditText)findViewById(R.id.t1);
            EditText numero2=(EditText)findViewById(R.id.t2);
            try{
                double num1=Double.parseDouble(numero1.getText().toString());
                double ris=num1*1.8+32;
                String risultato=""+ris;
                numero2.setText(risultato);
                }catch(NumberFormatException e){
                    e.printStackTrace();
                    numero2.setText("Devi inserire un numero!");
                }
        }
public void conv2(View view){
            EditText numero1=(EditText)findViewById(R.id.t1);
            EditText numero2=(EditText)findViewById(R.id.t2);
            try{
                           double num2=Double.parseDouble(numero2.getText().toString());
            double ris=(num2-32)/1.8;
            String risultato=""+ris;
            numero1.setText(risultato);
            }catch(NumberFormatException e){
                e.printStackTrace();
                numero1.setText("Devi inserire un numero!");
            }
}
}

-----------------main.xml-----------------

<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
    <TableRow>
    <EditText
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:id="@+id/t1"
    />
            <TextView android:text=" °C"
                  android:layout_width="100px"
                  />
    </TableRow>
        <TableRow>
    <EditText
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:id="@+id/t2"
    />
            <TextView android:text=" °F"
                  android:layout_width="100px"
                  />
    </TableRow>
        <Button android:id="@+id/converti"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="converti in °F"
                android:onClick="conv1"
          />
        <Button android:id="@+id/converti2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="converti in °C"
                android:onClick="conv2"
          />
</TableLayout>

 Senza titoloSenza titolo2

Commenti