----------------Calcolatrice.java----------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.me.calcolatrice;
import android.app.Activity;
import android.os.Bundle;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.content.Context;
import android.widget.*;
import android.view.View;
import android.content.Intent;
/**
*
* @author 4ALE
*/
public class Calcolatrice 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 double leggiV1(){
EditText numero1=(EditText)findViewById(R.id.num1);
double num1=0;
try{
num1=Double.parseDouble(numero1.getText().toString());
}catch(Exception e){
numero1.setError("Il dato immesso non è valido");
}
return num1;
}
public double leggiV2(){
EditText numero2=(EditText)findViewById(R.id.num2);
double num2=0;
try{
num2=Double.parseDouble(numero2.getText().toString());
}catch(Exception e){
numero2.setError("Il dato immesso non è valido");
}
return num2;
}
public void somma(View view) {
double a=leggiV1();
double b=leggiV2();
double c=a+b;
setRisp(a+"+"+b+"="+c);
}
public void cos (View view){
double num1=leggiV1();
double ang=Math.toRadians(num1);
double cos=Math.cos(ang);
setRisp("cos"+num1+"="+cos);
}
public void sen (View view){
double num1=leggiV1();
double ang=Math.toRadians(num1);
double sin=Math.sin(ang);
setRisp("sin"+num1+"="+sin);
}
public void setRisp(String risp){
TextView ris1=(TextView)findViewById(R.id.ris);
ris1.setText(risp);
}
public void diff(View view){
double a=leggiV1();
double b=leggiV2();
double c=a-b;
setRisp(a+"-"+b+"="+c);
}
public void per(View view){
double a=leggiV1();
double b=leggiV2();
double c=a*b;
setRisp(a+"x"+b+"="+c);
}
public void divi(View view){
double a=leggiV1();
double b=leggiV2();
double c=a/b;
setRisp(a+"/"+b+"="+c);
}
public void tan(View view){
double a=leggiV1();
double tan=Math.tan(Math.toRadians(a));
setRisp("tan"+a+"="+tan);
}
public void log(View view){
double a=leggiV1();
double l=Math.log(a);
setRisp("log"+a+"="+l);
}
}
----------------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="5"
>
<TableRow>
<TextView android:text="numero:"
android:layout_width="100px"
/>
<EditText
android:layout_width="200px"
android:layout_height="wrap_content"
android:id="@+id/num1"
/>
</TableRow>
<TableRow>
<TextView android:text="numero:"
android:layout_width="100px"
/>
<EditText
android:layout_width="200px"
android:layout_height="wrap_content"
android:id="@+id/num2"
/>
</TableRow>
<TableRow>
<TextView android:text="Risultato:"
android:layout_width="100px"
/>
<TextView
android:layout_width="200px"
android:layout_height="30px"
android:id="@+id/ris"
/>
</TableRow>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="4"
>
<TableRow>
<Button android:id="@+id/somma"
android:layout_width="50px"
android:layout_height="50px"
android:text="+"
android:onClick="somma"
/>
<Button android:id="@+id/diff"
android:layout_width="50px"
android:layout_height="50px"
android:text="-"
android:onClick="diff"
/>
<Button android:id="@+id/prodotto"
android:layout_width="50px"
android:layout_height="50px"
android:text="x"
android:onClick="per"
/>
<Button android:id="@+id/rapporto"
android:layout_width="50px"
android:layout_height="50px"
android:text=":"
android:onClick="divi"
/>
</TableRow>
<TableRow>
<Button android:id="@+id/cos"
android:layout_width="70px"
android:layout_height="50px"
android:text="cos(x)"
android:onClick="cos"
/>
<Button android:id="@+id/sin"
android:layout_width="70px"
android:layout_height="50px"
android:text="sin(x)"
android:onClick="sen"
/>
<Button android:id="@+id/tan"
android:layout_width="70px"
android:layout_height="50px"
android:text="tan(x)"
android:onClick="tan"
/>
<Button android:id="@+id/log"
android:layout_width="70px"
android:layout_height="50px"
android:text="log(x)"
android:onClick="log"
/>
</TableRow>
</TableLayout>
</TableLayout>
Molto bene
RispondiElimina