dimanche 1 février 2015

Android Studio: Tuto QUIZZ

Cet article présente comment faire une application de type Quizz avec Android Studio.
 
 
1) commencer un nouveau projet.

 


 
On laisse les options par défaut.
Le nom du projet doit commencer par une majuscule !





 
2) le projet est crée.
 
On définit la position des widgets (2 boutons, et un champs texte)
 
 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="
http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/questiontext" />
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="True"
                android:id="@+id/buttonTrue" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="False"
                android:id="@+id/buttonFalse" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>
 
 
 
pour obtenir l'exécution du code, on définit la classe MainActivity.java comme ceci:
 
package com.tontonzition.tutoquizz;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
    static int randomTerm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String[]p1= new String[100];
        final int[] p2= new int[100];
        int i;
        final int lower = 1;
        final int higher = 9;
        p1[1] = "1 the answser is TRUE ";
        p2[1]= 1;
        p1[2] = "2 the answser is TRUE";
        p2[2]= 1;
        p1[3] = "3 the answser is TRUE";
        p2[3]= 1;
        p1[4] = "4 the answser is TRUE";
        p2[4]= 1;
        p1[5] = "5 the answser is TRUE";
        p2[5]= 1;
        p1[6] = "6 the answser is FALSE";
        p2[6]= 0;
        p1[7] = "7 the answser is FALSE";
        p2[7]= 0;
        p1[8] = "8 the answser is FALSE";
        p2[8]= 0;
        p1[9] = "9 the answser is FALSE";
        p2[9]= 0;
        final TextView texte  = (TextView) findViewById(R.id.questiontext);
        final Button boutonTrue  = (Button) findViewById(R.id.buttonTrue);
        final Button boutonFalse  = (Button) findViewById(R.id.buttonFalse);
        randomTerm = (int)(Math.random() * (higher+1-lower)) + lower;
        texte.setText(p1[randomTerm]);
 
        boutonTrue.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String message;
                if (p2[randomTerm]==1){
                  message = "Good response \n";
                }
                else
                {
                    message = "Bad response \n";
                }
                message = message + " other question \n";

                randomTerm = (int)(Math.random() * (higher+1-lower)) + lower;
                message = message + p1[randomTerm];

                texte.setText(message);
            }
        });

        boutonFalse.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String message;
                if (p2[randomTerm]==0){
                    message = "Good response \n";
                }
                else
                {
                    message = "Bad response \n";
                }
                message = message + " other question \n";

                randomTerm = (int) (Math.random() * (higher + 1 - lower)) + lower;
                message = message + p1[randomTerm];


                texte.setText(message);
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}



3) on lance l'émulateur, il faut parfois être patient, le lancement prend quelques minutes.
 Oui on a bien le résultat voulu .
 

Aucun commentaire:

Enregistrer un commentaire