Transformation de la checkbox pour l'option de la case vide en préférence

This commit is contained in:
Mathis CHAIGNEAU 2023-04-09 14:55:47 +02:00
parent 393cc799d3
commit e551bef3bf
17 changed files with 97 additions and 32 deletions

Binary file not shown.

View File

@ -15,7 +15,7 @@
android:name=".ChoixDuMotDePasse"
android:exported="false" />
<activity
android:name=".SettingActivity"
android:name=".ConfigurationActivity"
android:exported="false" />
<activity
android:name=".MasterMindActivity"

View File

@ -0,0 +1,15 @@
package com.example.mastermind;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class ConfigurationActivity extends PreferenceActivity {
public static String PREF_PIECE_VIDE = "pref_piece_vide";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.addPreferencesFromResource(R.xml.configuration_screen);
}
}

View File

@ -1,7 +1,11 @@
package com.example.mastermind;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

View File

@ -1,7 +1,11 @@
package com.example.mastermind;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
@ -10,14 +14,43 @@ import com.example.mastermind.controller.menu.ObservateurMenuDebutPartie;
import com.example.mastermind.util.UtilTypePartie;
public class MenuActivity extends AppCompatActivity {
private SharedPreferences prefs;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
setContentView(R.layout.activity_menu);
this.findViewById(R.id.unJoueur)
.setOnClickListener(new ObservateurMenuDebutPartie(this, UtilTypePartie.UN_JOEUR, (CheckBox) this.findViewById(R.id.vide)));
.setOnClickListener(new ObservateurMenuDebutPartie(this, UtilTypePartie.UN_JOEUR));
this.findViewById(R.id.deuxJoueur)
.setOnClickListener(new ObservateurMenuDebutPartie(this, UtilTypePartie.DEUX_JOEUR, (CheckBox) this.findViewById(R.id.vide)));
.setOnClickListener(new ObservateurMenuDebutPartie(this, UtilTypePartie.DEUX_JOEUR));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.getMenuInflater().inflate(R.menu.menu_configuration, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_configuration) {
this.startActivity(new Intent(this, ConfigurationActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean acceptPieceVide() {
return this.prefs.getBoolean(ConfigurationActivity.PREF_PIECE_VIDE, false);
}
}

View File

@ -1,42 +1,43 @@
package com.example.mastermind.controller.menu;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.CheckBox;
import androidx.appcompat.app.AppCompatActivity;
import com.example.mastermind.ChoixDuMotDePasse;
import com.example.mastermind.ConfigurationActivity;
import com.example.mastermind.MasterMindActivity;
import com.example.mastermind.MenuActivity;
import java.util.Random;
public class ObservateurMenuDebutPartie implements View.OnClickListener {
private AppCompatActivity menu;
private MenuActivity menu;
private int typePartie;
private CheckBox vide;
public ObservateurMenuDebutPartie(AppCompatActivity menu, int type, CheckBox vide){
public ObservateurMenuDebutPartie(MenuActivity menu, int type){
this.menu=menu;
this.typePartie=type;
this.vide=vide;
}
@Override
public void onClick(View view) {
boolean vide = this.menu.acceptPieceVide();
if(typePartie == 1){
Intent mastermind = new Intent(menu, MasterMindActivity.class);
mastermind.putExtra("vide", vide.isChecked());
mastermind.putExtra("vide", vide);
Random r=new Random();
int tab[]=new int[4];
int max=6;
if(vide.isChecked()){
if(vide){
max++;
}
tab[0]=r.nextInt(max);
@ -49,7 +50,7 @@ public class ObservateurMenuDebutPartie implements View.OnClickListener {
}
if(typePartie ==2){
Intent choixCode = new Intent(menu, ChoixDuMotDePasse.class);
choixCode.putExtra("vide", vide.isChecked());
choixCode.putExtra("vide", vide);
menu.startActivity(choixCode);
}
}

View File

@ -53,23 +53,4 @@
android:textSize="30dp"
android:text="Deux Joueur"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pièce vide"
android:layout_alignParentBottom="true"
android:textSize="30dp"
android:layout_toLeftOf="@+id/vide"
android:editable="false"
android:textColor="@color/white"
/>
<CheckBox
android:id="@+id/vide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@+id/action_configuration"
android:title="@string/action_configuration"
android:orderInCategory="100"
app:showAsAction="never"
/>
</menu>

View File

@ -1,3 +1,6 @@
<resources>
<string name="app_name">MasterMind</string>
<string name="action_configuration">Configuration</string>
<string name="pref_piece_vide_titre">Pièces vides</string>
<string name="pref_piece_vide_texte">Autoriser les pièces vides</string>
</resources>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
>
<PreferenceCategory
android:title="Général"
android:key="general"
>
<CheckBoxPreference
android:key="pref_piece_vide"
android:title="@string/pref_piece_vide_titre"
android:summary="@string/pref_piece_vide_texte"
android:defaultValue="false"
/>
</PreferenceCategory>
</PreferenceScreen>