يقدم موقعنا تمارين مصححة في لغات البرمجة مثل C / C ++ و PHP
. اكتشف أيضًا مدونتنا التي تحتوي على آخر أخبار التكنولوجيا من هنا

abdelouafi

Administrator
أحدث المقالات
إذا كنت ترغب في مشاهدة الصور ، يرجى النقر عليها
Blog
SUIVEZ NOTRE CHAINE YOUTUBE: قم بالتسجيل في قناتنا عبر هذا الرابط https://www.youtube.com/channel/UCCITRMWPcElh-96wCS3EyUg
devoir 1 math tronc commun

مجموعة من دروس و فروض جميع المستويات

دروس الإعدادي - دروس الثانوي الثأهيلي - دروس التعليم الابتدائي - فروض مختلف المستويات الدراسية

فضلا و ليس أمرا شارك هذه الصفحة مع أصدقائك:
On va voir comment réaliser une calculatrice sous Android Studio:

Réaliser l'interface graphique suivante:

upload_2018-2-14_13-22-7.png




Pour faire cette interface graphique, modifier le code source du fichier xml Activity_main.xml comme ceci:

Code:
<?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"
    >

    <EditText android:id="@+id/EditText01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20px"
        android:editable="false"
        android:cursorVisible="false"
        />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

        <Button android:id="@+id/button1"
            android:text="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

        <Button android:id="@+id/button2"
            android:text="2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

        <Button android:id="@+id/button3"
            android:text="3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

        <Button android:id="@+id/buttonPlus"
            android:text="+"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />
    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

        <Button android:id="@+id/button4"
            android:text="4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <Button android:id="@+id/button5"
            android:text="5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

        <Button android:id="@+id/button6"
            android:text="6"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

        <Button android:id="@+id/buttonMoins"
            android:text="-"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

        <Button android:id="@+id/button7"
            android:text="7"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

        <Button android:id="@+id/button8"
            android:text="8"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

        <Button android:id="@+id/button9"
            android:text="9"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

        <Button android:id="@+id/buttonMultiplier"
            android:text="*"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

        <Button android:id="@+id/button0"
            android:text="0"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

        <Button android:id="@+id/buttonPoint"
            android:text="."
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

        <Button android:id="@+id/buttonC"
            android:text="C"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

        <Button android:id="@+id/buttonDivision"
            android:text="/"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

    </LinearLayout>

    <Button android:id="@+id/buttonEgal"
        android:text="="
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

    <ImageView android:id="@+id/logo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/logo_tuto_mobile"
        android:paddingTop="20px"
        />
</LinearLayout>


Le code source du fichier MainActivity.java est :

Code:
package test1.ista.main.calculatrice_project;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;




public class MainActivity extends AppCompatActivity {

    //déclaration des buttons
    Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bplus,bmoins,betoile,bdiv,bc,begal,bpoint;
    EditText res;
    private double chiffre1;
    private boolean clicOperateur = false;
    private boolean update = false;
    private String operateur = "";





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


        b0 = (Button) findViewById(R.id.button0);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        b5 = (Button) findViewById(R.id.button5);

        b6 = (Button) findViewById(R.id.button6);
        b7 = (Button) findViewById(R.id.button7);
        b8 = (Button) findViewById(R.id.button8);
        b9 = (Button) findViewById(R.id.button9);
        bplus = (Button) findViewById(R.id.buttonPlus);
        bmoins = (Button) findViewById(R.id.buttonMoins);
        betoile = (Button) findViewById(R.id.buttonMultiplier);
        bdiv = (Button) findViewById(R.id.buttonDivision);
        bc = (Button) findViewById(R.id.buttonC);
        begal = (Button) findViewById(R.id.buttonEgal);
        bpoint = (Button) findViewById(R.id.buttonPoint);

        res = (EditText) findViewById(R.id.EditText01);

        // opération d'addition
        bplus.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                plusClick();
            }
        });

        //opération Mopins
        bmoins.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                moinsClick();
            }
        });


        //opération multiplication
        betoile.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                etoileClick();
            }
        });

        // opération de division
        bdiv.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                divClick();
            }
        });


        bc.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                resetClick();
            }
        });

        begal.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                egalClick();
            }
        });

        bpoint.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                chiffreClick(".");
            }
        });


        b0.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                chiffreClick("0");
            }
        });

        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                chiffreClick("1");
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                chiffreClick("2");
            }
        });

        b3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                chiffreClick("3");
            }
        });

        b4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                chiffreClick("4");
            }
        });

        b8.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                chiffreClick("5");
            }
        });

        b6.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                chiffreClick("6");
            }
        });

        b7.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                chiffreClick("7");
            }
        });

        b8.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                chiffreClick("8");
            }
        });

        b9.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                chiffreClick("9");
            }
        });

    }
    public void chiffreClick(String str) {
        if(update){
            update = false;
        }else{
            if(!res.getText().equals("0"))
                str = res.getText() + str;
        }
        res.setText(str);



    }


    public void plusClick(){

        if(clicOperateur){
            calcul();
            res.setText(String.valueOf(chiffre1));
        }else{
            chiffre1 = Double.valueOf(res.getText().toString()).doubleValue();
            clicOperateur = true;
        }
        operateur = "+";
        update = true;
    }




    public void moinsClick(){
        if(clicOperateur){
            calcul();
            res.setText(String.valueOf(chiffre1));
        }else{
            chiffre1 = Double.valueOf(res.getText().toString()).doubleValue();
            clicOperateur = true;
        }
        operateur = "-";
        update = true;
    }

    //voici la méthode qui est  exécutée lorsqu'on clique sur le bouton *
    public void etoileClick(){
        if(clicOperateur){
            calcul();
            res.setText(String.valueOf(chiffre1));
        }else{
            chiffre1 = Double.valueOf(res.getText().toString()).doubleValue();
            clicOperateur = true;
        }
        operateur = "*";
        update = true;
    }


    public void divClick(){
        if(clicOperateur){
            calcul();
            res.setText(String.valueOf(chiffre1));
        }else{
            chiffre1 = Double.valueOf(res.getText().toString()).doubleValue();
            clicOperateur = true;
        }
        operateur = "/";
        update = true;
    }


    //voici la méthode qui est  exécutée lorsqu'on clique sur le bouton =
    public void egalClick(){
        calcul();
        update = true;
        clicOperateur = false;
    }

    //voici la méthode qui est  exécutée lorsque l'on clique sur le bouton C
    public void resetClick(){
        clicOperateur = false;
        update = true;
        chiffre1 = 0;
        operateur = "";
        res.setText("");
    }

    //Voici la méthode qui fait le calcul qui a été demandé par l'utilisateur
    private void calcul(){
        if(operateur.equals("+")){
            chiffre1 = chiffre1 + Double.valueOf(res.getText().toString()).doubleValue();
            res.setText(String.valueOf(chiffre1));
        }

        if(operateur.equals("-")){
            chiffre1 = chiffre1 - Double.valueOf(res.getText().toString()).doubleValue();
            res.setText(String.valueOf(chiffre1));
        }

        if(operateur.equals("*")){
            chiffre1 = chiffre1 * Double.valueOf(res.getText().toString()).doubleValue();
            res.setText(String.valueOf(chiffre1));
        }

        if(operateur.equals("/")){
            try{
                chiffre1 = chiffre1 / Double.valueOf(res.getText().toString()).doubleValue();
                res.setText(String.valueOf(chiffre1));
            }catch(ArithmeticException e){
                res.setText("0");
            }
        }
    }




}
 
Top