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

This commit is contained in:
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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -15,7 +15,7 @@
android:name=".ChoixDuMotDePasse" android:name=".ChoixDuMotDePasse"
android:exported="false" /> android:exported="false" />
<activity <activity
android:name=".SettingActivity" android:name=".ConfigurationActivity"
android:exported="false" /> android:exported="false" />
<activity <activity
android:name=".MasterMindActivity" android:name=".MasterMindActivity"
@@ -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);
}
}
@@ -1,7 +1,11 @@
package com.example.mastermind; package com.example.mastermind;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@@ -1,7 +1,11 @@
package com.example.mastermind; package com.example.mastermind;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; 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.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@@ -10,14 +14,43 @@ import com.example.mastermind.controller.menu.ObservateurMenuDebutPartie;
import com.example.mastermind.util.UtilTypePartie; import com.example.mastermind.util.UtilTypePartie;
public class MenuActivity extends AppCompatActivity { public class MenuActivity extends AppCompatActivity {
private SharedPreferences prefs;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
setContentView(R.layout.activity_menu); setContentView(R.layout.activity_menu);
this.findViewById(R.id.unJoueur) 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) 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);
}
} }
@@ -1,42 +1,43 @@
package com.example.mastermind.controller.menu; package com.example.mastermind.controller.menu;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View; import android.view.View;
import android.widget.CheckBox; import android.widget.CheckBox;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import com.example.mastermind.ChoixDuMotDePasse; import com.example.mastermind.ChoixDuMotDePasse;
import com.example.mastermind.ConfigurationActivity;
import com.example.mastermind.MasterMindActivity; import com.example.mastermind.MasterMindActivity;
import com.example.mastermind.MenuActivity;
import java.util.Random; import java.util.Random;
public class ObservateurMenuDebutPartie implements View.OnClickListener { public class ObservateurMenuDebutPartie implements View.OnClickListener {
private AppCompatActivity menu; private MenuActivity menu;
private int typePartie; private int typePartie;
private CheckBox vide; public ObservateurMenuDebutPartie(MenuActivity menu, int type){
public ObservateurMenuDebutPartie(AppCompatActivity menu, int type, CheckBox vide){
this.menu=menu; this.menu=menu;
this.typePartie=type; this.typePartie=type;
this.vide=vide;
} }
@Override @Override
public void onClick(View view) { public void onClick(View view) {
boolean vide = this.menu.acceptPieceVide();
if(typePartie == 1){ if(typePartie == 1){
Intent mastermind = new Intent(menu, MasterMindActivity.class); Intent mastermind = new Intent(menu, MasterMindActivity.class);
mastermind.putExtra("vide", vide.isChecked()); mastermind.putExtra("vide", vide);
Random r=new Random(); Random r=new Random();
int tab[]=new int[4]; int tab[]=new int[4];
int max=6; int max=6;
if(vide.isChecked()){ if(vide){
max++; max++;
} }
tab[0]=r.nextInt(max); tab[0]=r.nextInt(max);
@@ -49,7 +50,7 @@ public class ObservateurMenuDebutPartie implements View.OnClickListener {
} }
if(typePartie ==2){ if(typePartie ==2){
Intent choixCode = new Intent(menu, ChoixDuMotDePasse.class); Intent choixCode = new Intent(menu, ChoixDuMotDePasse.class);
choixCode.putExtra("vide", vide.isChecked()); choixCode.putExtra("vide", vide);
menu.startActivity(choixCode); menu.startActivity(choixCode);
} }
} }
-19
View File
@@ -53,23 +53,4 @@
android:textSize="30dp" android:textSize="30dp"
android:text="Deux Joueur"/> 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> </RelativeLayout>
@@ -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>
+3
View File
@@ -1,3 +1,6 @@
<resources> <resources>
<string name="app_name">MasterMind</string> <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> </resources>
@@ -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>