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 : Quizz Animaux

Créer un Jeux de quizz

1.Layout

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFEB3B"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:id="@+id/motimage"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff00">


<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/editmot0"
android:background="#fff"
android:layout_margin="2dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/editmot1"
android:background="#fff"
android:layout_margin="2dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/editmot2"
android:background="#fff"
android:layout_margin="2dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/editmot3"
android:background="#fff"
android:layout_margin="2dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/editmot4"
android:background="#fff"
android:layout_margin="2dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/editmot5"
android:background="#fff"
android:layout_margin="2dp"/>

</TableRow>

</TableLayout>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="17dp"
android:text="Valider"
android:id="@+id/bmotValider"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="17dp"
android:text=""
android:gravity="center_horizontal"
android:backgroundTint="#ff00"
android:id="@+id/textmot"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="17dp"
android:text="Start"
android:backgroundTint="#ff00"
android:id="@+id/bmotStart"/>



</LinearLayout>


2.Java


import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class LesmotsCroises extends AppCompatActivity implements View.OnClickListener {
EditText[]lescase=new EditText[6];
ImageView image;
Button bmotStart,bmotValider;
Integer []mesimages= {R.drawable.cow,R.drawable.horse,R.drawable.zebra,R.drawable.camel};
String []nomimages={"COW","HORSE","ZEBRA","CAMEL"};
int positionChoisi=0;
String motChoisi="";
int nombreLettre=0;
TextView textmot;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lesmots_croises);
image=findViewById(R.id.motimage);
bmotStart=findViewById(R.id.bmotStart);
bmotValider=findViewById(R.id.bmotValider);
textmot=findViewById(R.id.textmot);
for(int i=0;i<6;i++)
{
int id=getResources().getIdentifier("editmot"+i,"id",getPackageName());
lescase[i]=findViewById(id);
}
bmotStart.setOnClickListener(this);
bmotValider.setOnClickListener(this);
jouer();

}

@Override
public void onClick(View view) {
if(bmotStart.getId()==view.getId())
{
jouer();
}

if(bmotValider.getId()==view.getId())
{
String motEcris="";
for(int i=0;i<=nombreLettre;i++)
{
motEcris+=lescase[i].getText().toString();
}
Log.d("motEcris:::::::",motEcris);
Log.d("motChoisi:::::::",motChoisi);

if(motEcris.equalsIgnoreCase(motChoisi))
{
textmot.setText("Good");
textmot.setTextColor(Color.GREEN);
jouer();
}
else
{
textmot.setText("NO");
textmot.setTextColor(Color.RED);
}


}
}

public void jouer()
{
//vider les editText
for(int i=0;i<6;i++)
{
lescase[i].setText("");
}
positionChoisi=(int)Math.floor(Math.random()*3);
image.setImageResource(mesimages[positionChoisi]);
motChoisi=nomimages[positionChoisi];
nombreLettre=motChoisi.length()-1;

//Afficher le première caractères
int caseEditAafficher=(int)Math.floor(Math.random()*nombreLettre);
char lettreChosi=motChoisi.charAt(caseEditAafficher);
lescase[caseEditAafficher].setText(""+lettreChosi);

//Afficher le deuxième caractères
boolean caseTrouver=false;
while(caseTrouver==false)
{

int caseEditAafficher2=(int)Math.floor(Math.random()*nombreLettre);
if(caseEditAafficher2!=caseEditAafficher) {
lettreChosi = motChoisi.charAt(caseEditAafficher2);
lescase[caseEditAafficher2].setText(""+lettreChosi);
caseTrouver=true;
}

}

Log.d("motChoisi:::::::",motChoisi);

}
}