3 pièce(s) jointe(s)
Changement d'image en MVC
Bonjour ,
Bon j'ai juste un petit souci , je débute en développement Android , j'ai un TP a rendre pour ma séance prochaine , le principe est simple , il faudra entrer le nom du joueur après il y as une nouvelle activité qui s'ouvre , et on doit cliquer sur le bouton pour que l'image change , a chaque clique ! bon j'ai réussis mettre en place tout mes interfaces , j'ai mis un intent en relation avec ma deuxième activité , mais quand je clique sur le bouton rien ne se passe !
bon la le principe c'est de travailler en modèle MVC , voici l’arborescence de mon projet :
Pièce jointe 202542
La j'ai le code de ma MainActivity et son interface graphique :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package appgame.storycompany.com.gamechoice.Controleur;
...
public class MainActivity extends AppCompatActivity {
private EditText nom_joueur;
private Button button_envoyer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nom_joueur = (EditText) findViewById(R.id.editText);
button_envoyer = (Button) findViewById(R.id.button);
button_envoyer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String nom_j = nom_joueur.getText().toString();
EnvoyerNom(nom_j);
}
});
}
private void EnvoyerNom(String nom)
{
Intent intent_send = new Intent(this,GameActivity.class);
intent_send.putExtra(getString(R.string.editText_nom_ressource),nom);
startActivity(intent_send);
}
} |
Pièce jointe 202543
La j'ai le code de ma deuxième activité et son interface :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| package appgame.storycompany.com.gamechoice.Controleur;
....
public class GameActivity extends AppCompatActivity {
private TextView nom_joueur_textview;
private Button next_game_button;
private ImageView game_image;
private Game game = new Game();
private Page page_game_courrante ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
nom_joueur_textview = (TextView) findViewById(R.id.textView);
next_game_button = (Button) findViewById(R.id.button2);
game_image = (ImageView) findViewById(R.id.imageView);
Intent recup_nom = getIntent();
String nomJoueur = recup_nom.getStringExtra(getString(R.string.editText_nom_ressource));
if(nomJoueur.isEmpty()){
nomJoueur = "Player1";
}
nom_joueur_textview.setText(nomJoueur);
loadGamePage(0);
}
private void loadGamePage(int id_choice_page)
{
page_game_courrante = game.getPage(id_choice_page);
Drawable drawable = getResources().getDrawable(page_game_courrante.getId_image_game());
game_image.setImageDrawable(drawable);
next_game_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int nextPage = page_game_courrante.getChoice().getNextPage();
loadGamePage(nextPage);
}
});
}
} |
Pièce jointe 202544
La j'ai mais 3 classes Modèle que j'ai utilisé pour mettre en place ma page , avec les images , le choix .. :
Choise.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package appgame.storycompany.com.gamechoice.Model;
public class Choice {
private int nextPage;
public Choice(int nextPage) {
this.nextPage = nextPage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
} |
Page.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package appgame.storycompany.com.gamechoice.Model;
public class Page {
private int id_image_game;
private Choice choice;
public Page(int id_image_game, Choice choice) {
this.id_image_game = id_image_game;
this.choice = choice;
}
public int getId_image_game() {
return id_image_game;
}
public void setId_image_game(int id_image_game) {
this.id_image_game = id_image_game;
}
public Choice getChoice() {
return choice;
}
public void setChoice(Choice choice) {
this.choice = choice;
}
} |
Game.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package appgame.storycompany.com.gamechoice.Model;
import appgame.storycompany.com.gamechoice.R;
public class Game {
private Page[] gamePage;
public Game(){
gamePage = new Page[3];
gamePage[0] = new Page(R.drawable.game3,new Choice(0));
gamePage[1] = new Page(R.drawable.game2,new Choice(1));
gamePage[2] = new Page(R.drawable.game1,new Choice(2));
}
public Page getPage(int pageNumber) {
return gamePage[pageNumber];
}
} |