In questo tutorial andremo a progettare il front-end per la nostra app calcolatrice.
Ovviamente ognuno di voi andrà a modificare
il layout a proprio piacimento:
<?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent”> <TextView android:id=”@+id/editText1″ android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_margin=”10dp” android:inputType=”numberDecimal” android:textAlignment=”textEnd”/> <TextView android:id=”@+id/editText2″ android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_margin=”10dp” android:inputType=”numberDecimal” android:textAlignment=”textEnd”/> <TableRow android:gravity=”center_horizontal” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_margin=”5dp”> <Button android:text=”C” ” android:layout_width=”0dp” android:id=”@+id/buttonCanc” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”…..”/> <Button android:text=”0″ android:id=”@+id/buttonZero” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”…..”/> <Button android:text=”ײ” android:id=”@+id/buttonP3″ android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”…..”/> <Button android:text=”÷” android:id=”@+id/buttonDiviso” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”…..”/> </TableRow> <TableRow android:gravity=”center_horizontal” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_margin=”5dp”> <Button android:text=”7″ android:id=”@+id/buttonSette” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”…….”/> <Button android:text=”8″ android:id=”@+id/buttonOtto” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”…….”/> <Button android:text=”9″ android:id=”@+id/buttonNove” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”……”/> <Button android:text=”×” android:id=”@+id/buttonPer” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”……”/> </TableRow> <TableRow android:gravity=”center_horizontal” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_margin=”5dp”> <Button android:text=”4″ android:id=”@+id/buttonQuattro” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”……”/> <Button android:text=”5″ android:id=”@+id/buttonCinque” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”……”/> <Button android:text=”6″ android:id=”@+id/buttonSei” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”……”/> <Button android:text=”-” android:id=”@+id/buttonMeno” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”……”/> </TableRow> <TableRow android:gravity=”center_horizontal” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_margin=”5dp”> <Button android:text=”1″ android:id=”@+id/buttonUno” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”……”/> <Button android:text=”2″ android:id=”@+id/buttonDue” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”…..”/> <Button android:text=”3″ android:id=”@+id/buttonTre” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”…..”/> <Button android:text=”+” android:id=”@+id/buttonPiu” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”…..”/> </TableRow> <TableRow android:gravity=”center_horizontal” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_margin=”5dp”> <Button android:text=”.” android:id=”@+id/buttonVirgola” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”…..”/> <Button android:text=”=” android:id=”@+id/buttonUguale” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_margin=”3dp” android:layout_gravity=”center_horizontal” android:layout_weight=”1″ android:background=”…..”/> </TableRow> </LinearLayout>
Ora dobbiamo aggiungere il nostro codice Java nel nostro file MainActivity.java. package com.myexsample.calculator.activities;
import android.widget.Button;
import android.view.View;
import android.os.Bundle;
import android.widget.TextView.; import androidx.appcompat.app.;
import android.widget.*;
import androidx.appcompat.R; public class MainActivity extends AppCompatActivity
{
private TextView textUno;
private TextView textDue;
private Button bPiu;
private Button bMeno;
private Button bPer;
private Button bDiviso;
private Button bZero;
private Button bUguale;
private Button bVirgola;
private Button bCancel;
private Button pTre;
private Button bUno;
private Button bDue;
private Button bTre;
private Button bQuattro;
private Button bCinque;
private Button bSei;
private Button bSette;
private Button bOtto;
private Button bNove; private char ACTION;
private double ValoreUno= Double.NaN;
private double ValoreDue;
private final char Addizione= ‘+’;
private final char Sottrazione = ‘-‘;
private final char Divisione = ‘÷’;
private final char Moltiplicazione= ‘×’; private final char EQU = ‘=’;
@Override protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); textUno=findViewById(R.id.editText2);
textDue=findViewById(R.id.editText1);
bPiu=findViewById(R.id.buttonPiu);
bMeno=findViewById(R.id.buttonMeno);
bPer=findViewById(R.id.buttonPer);
bDiviso=findViewById(R.id.buttonDiviso);
bZero=findViewById(R.id.buttonZero);
bUguale=findViewById(R.id.buttonUguale);
bVirgola=findViewById(R.id.buttonVirgola);
bCancel=findViewById(R.id.buttonCanc);
pTre=findViewById(R.id.buttonP3);
bUno=findViewById(R.id.buttonUno);
bDue=findViewById(R.id.buttonDue);
bTre=findViewById(R.id.buttonTre);
bQuattro=findViewById(R.id.buttonQuattro);
bCinque=findViewById(R.id.buttonCinque);
bSei=findViewById(R.id.buttonSei);
bSette=findViewById(R.id.buttonSette);
bOtto=findViewById(R.id.buttonOtto);
bNove=findViewById(R.id.buttonNove); bUno.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ifErrorOnOutput(); textUno.setText(textUno.getText()+ “1”);
}
});
bDue.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ifErrorOnOutput(); textUno.setText(textUno.getText()+”2″);
}
});
bTre.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ifErrorOnOutput(); textUno.setText(textUno.getText()+”3″);
}
});
bQuattro.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ifErrorOnOutput(); textUno.setText(textUno.getText()+”4″);
}
});
bCinque.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ifErrorOnOutput(); textUno.setText(textUno.getText()+”5″);
}
}); bSei.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ifErrorOnOutput(); textUno.setText(textUno.getText()+”6″);
}
});
bSette.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ifErrorOnOutput(); textUno.setText(textUno.getText()+”7″);
}
});
bOtto.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ifErrorOnOutput(); textUno.setText(textUno.getText()+”8″);
}
});
bNove.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ifErrorOnOutput(); textUno.setText(textUno.getText()+”9″);
}
});
bZero.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ifErrorOnOutput(); textUno.setText(textUno.getText()+”0″);
}
}
}); bPiu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (textUno.getText().length() >0){
ACTION = Addizione;
if (textUno.getText().length() > 0)
if (!ifReallyDecimal()) {
textDue.setText(ValoreUno + “+”);
} else {
textDue.setText((int)ValoreUno+”+”);
}
textUno.setText(null);
} else {
textDue.setText(“Error”);
}
}
});
bMeno.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (textUno.getText().length() > 0) {
ACTION =Sottrazione;
if (textUno.getText().length() > 0)
if (!ifReallyDecimal()) {
textDue.setText(ValoreUno + “-“);
} else {
textDue.setText((int) ValoreUno + “-“);
}
textUno.setText(null);
} else {
textDue.setText(“Error”);
}
}
});
bPer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (textUno.getText().length() > 0) {
ACTION =Moltiplicazione;
if (textUno.getText().length() > 0)
if (!ifReallyDecimal()) {
textDue.setText(ValoreUno + “×”);
} else {
textDue.setText((int) ValoreUno + “×”);
}
textUno.setText(null);
} else {
textDue.setText(“Error”);
}
}
});
bDiviso.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { if(textUno.getText().length() > 0) {
ACTION =Divisione;
if (textUno.getText().length() > 0)
if (!ifReallyDecimal()) {
textDue.setText(ValoreUno + “÷”);
} else {
textDue.setText((int) ValoreUno + “÷”);
}
textUno.setText(null);
} else {
textDue.setText(“Error”);
}
}
});
PUno . setOnClickListener ( new View . OnClickListener () {
@ Override
public void onClick ( View v ) {
if(textUno.getText().length() > 0) {
String val = textUno . getText (). toString ();
double r = Math . sqrt ( Double . parseDouble ( val ));
textUno . setText ( String . valueOf ( r ));
ValoreDue = Double.NaN;
} else {
ValoreUno = Double.NaN;
textUno.setText(“”);
textDue.setText(“”); }
}
});
pDue . setOnClickListener ( new View . OnClickListener () {
@ Override
public void onClick ( View v ) { if(textUno.getText().length() > 0) { String val = textUno . getText (). toString ();
double r = Math . cbrt ( Double . parseDouble ( val ));
textUno . setText ( String . valueOf ( r)); } else {
ValoreUno = Double.NaN; textUno.setText(“”);
textDue.setText(“”);
}
}
});
pTre. setOnClickListener ( new View . OnClickListener () {
@Override
public void onClick (View v ) {
if(textUno.getText().length() > 0) {
double d = Double . parseDouble ( textUno. getText (). toString ());
double quadrato = d * d ;
textUno. setText ( String . valueOf ( quadrato ));
textDue . setText ( d + “²” );
} else {
ValoreUno = Double.NaN; textUno.setText(“”);
textDue.setText(“”);
}
}
});
}
bUguale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (textDue.getText().length() > 0) {
ACTION = EQU;
if (!ifReallyDecimal()) {
textUno.setText(String.valueOf(ValoreUno));
} else {
textUno.setText(String.valueOf((int) ValoreUno));
}
textDue.setText(null);
} else {
textUno.setText(“Error”);
}
}
}); bCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (textUno.getText().length() > 0) {
CharSequence name = textUno.getText().toString();
textUno.setText(name.subSequence(0, name.length() – 1));
} else {
ValoreUno = Double.NaN;
ValoreDue = Double.NaN;
textUno.setText(“”);
textDue.setText(“”);
}
}
}); bCancel.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
ValoreUno = Double.NaN;
ValoreDue = Double.NaN;
textUno.setText(“”);
textDue.setText(“”);
return true;
}
});
bVirgola.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(textUno.getText().length() > 0) {
textUno.setText(textUno.getText()+”.”);
} else {
ValoreUno = Double.NaN;
textUno.setText(“”);
textDue.setText(“”);
}
}
});
Bene la nostra calcolatrice è pronta.
Alla prossima!!!