menu FixWins.com Spring MVC Spring Boot NodeJS+ExpressJS+Mysql ReactJS+NodeJS+Mysql ReactJS+Redux :CRUD Android PFE
Spring MVC Spring Boot NodeJS+ExpressJS+Mysql ReactJS+NodeJS+Mysql ReactJS+Redux :CRUD PFE

Android : Calcul a+b

Créer une application de calcul simple permettant de calcuer a+b

1.Layout


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingHorizontal="55dp"
android:weightSum="3"
android:background="#D8FDF3">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="300dp"
android:layout_weight="0.1">

<EditText
android:id="@+id/x"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginHorizontal="10dp"
android:layout_weight="2"
android:inputType="number"
android:backgroundTint="#09BC8A"
android:hint="X"
android:textAlignment="center"
android:textColor="#000"
android:textSize="25dp" />

<requestFocus />

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="+"
android:textSize="25dp"
android:gravity="center"
android:layout_weight="0.6"
android:backgroundTint="#D8FDF3"
android:textColor="#004346"

android:layout_marginHorizontal="10dp"/>

<EditText
android:id="@+id/y"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:hint="Y"
android:inputType="number"
android:textAlignment="center"
android:backgroundTint="#09BC8A"
android:textColor="#000"
android:layout_weight="2"
android:textSize="25dp"
android:layout_marginHorizontal="10dp"/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">

<Button
android:id="@+id/equal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="0"
android:backgroundTint="#004346"
android:paddingHorizontal="30dp"
android:text="="
android:textColor="#09BC8A"
android:textSize="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">

<TextView
android:id="@+id/resultat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textAlignment="center"
android:textSize="60dp"
android:textColor="#09BC8A"/>

</LinearLayout>

</LinearLayout>

2.Java


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class CalculApusBActivity extends AppCompatActivity implements View.OnClickListener {

//1.Délarer les objet java
EditText x_input, y_input;
Button equal_button;
TextView resultat;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//2.Lier les objet graphique avec les objet java
x_input = findViewById(R.id.x);
y_input = findViewById(R.id.y);
equal_button = findViewById(R.id.equal);
resultat = findViewById(R.id.resultat);
//3.Ecouter les boutons
equal_button.setOnClickListener(this);

}



@Override
public void onClick(View view) {
if(view.getId()==equal_button.getId())
{
double x1, y1, result;
if (!x_input.getText().toString().isEmpty() && !y_input.getText().toString().isEmpty()){
x1 = Double.parseDouble(x_input.getText().toString());
y1 = Double.parseDouble(y_input.getText().toString());
result = x1 + y1;
resultat.setText(String.valueOf(result));
} else {
resultat.setText("champs obligatoires");
resultat.setTextSize(18);
}
}
}
}