ups ^^
1package de.florian.spiel;
2
3import android.app.Activity;
4import android.os.Bundle;
5import android.view.Menu;
6import android.view.View;
7import android.widget.Button;
8
9
10public class MainActivity extends Activity
11implements android.view.View.OnClickListener {
12 Spiellogik spiel;
13
14
15
16
17
18 @Override
19 protected void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.activity_main);
22
23 //Spiellogik istanziieren
24 spiel = new Spiellogik();
25 //Antwort-Buttons mit Ereignislistener verbinden
26 for (int n = 1; n <= 4; n++) {
27 Button btn = null;
28
29
30
31 switch (n) {
32 case 1:
33 btn = (Button) this.findViewById(R.id.antwort1);
34 btn.setOnClickListener(this);
35 break;
36 case 2:
37 btn = (Button) this.findViewById(R.id.antwort2);
38 btn.setOnClickListener(this);
39 break;
40 case 3:
41 btn = (Button) this.findViewById(R.id.antwort3);
42 btn.setOnClickListener(this);
43 break;
44 case 4:
45 btn = (Button) this.findViewById(R.id.antwort4);
46 btn.setOnClickListener(this);
47 break;
48 }
49 }
50
51 //1.Frage laden
52 spiel.fragen[spiel.aktFrage].anzeigen(this);
53 }
54
55 public void OnClick(View v) {
56 int id = v.getId();
57 if (id == R.id.antwort1)
58 spiel.auswerten(1, this);
59 // spielAuswerten(1);
60 else if (id == R.id.antwort2)
61 spiel.auswerten(2, this);
62 // spielAuswerten(2);
63 else if (id == R.id.antwort3)
64 spiel.auswerten(3, this);
65 // spielAuswerten(3);
66 else if (id == R.id.antwort4)
67 spiel.auswerten(4, this);
68 // spielAuswerten(4);
69
70 }
71
72
73
74
75
76
77
78 @Override
79 public boolean onCreateOptionsMenu(Menu menu) {
80 // Inflate the menu; this adds items to the action bar if it is present.
81 getMenuInflater().inflate(R.menu.main, menu);
82 return true;
83 }
84
85 @Override
86 public void onClick(View arg0) {
87 // TODO Auto-generated method stub
88
89 }
90
91}
das ist die MainActivity
1package de.florian.spiel;
23import de.florian.spiel.R.id;
4import android.app.Activity;
5import android.widget.Button;
6import android.widget.Toast;
789class Spiellogik {
10final int ANZAHL_FRAGEN = 8;
11Frage[] fragen = new Frage [ANZAHL_FRAGEN];
12byte aktFrage = 0;
13int gewinnstufe = 0;
1415161718192021Spiellogik(){
22//Fragen erzeugen
23fragen[0] =
24new Frage("Wer wohnt bei Sheldon?",
25"Penny", "Howard", "Lennard", "niemand", 3);
2627fragen[1] =
28new Frage("Was sagt Sheldon wenn er ein Witz macht",
29"War ein Witz", "Joke", "Spaß", "Bazinga", 4);
3031// und so fort
3233}
34void auswerten(int schalter, Activity MainActivity) {
3536 37 3839if (!fragen[aktFrage].richtig(schalter)) {
40 //falsche Antwort
41 if(gewinnstufe == 0){
42 Toast.makeText(MainActivity, "Leider Falsch",
43 Toast.LENGTH_LONG).show();
44 45 }
46 else{
47 String str = "Sie haben Gewinnstufe " + gewinnstufe + "eirreicht!
";
48 Toast.makeText(MainActivity, str, Toast.LENGTH_LONG).show();
49 }
50 //Schalter deaktivieren
51 ((Button) MainActivity.findViewById(id.antwort1)).setEnabled(false);
52 ((Button) MainActivity.findViewById(id.antwort2)).setEnabled(false);
53 ((Button) MainActivity.findViewById(id.antwort3)).setEnabled(false);
54 ((Button) MainActivity.findViewById(id.antwort4)).setEnabled(false);
55 56} else {
575859 if(aktFrage< ANZAHL_FRAGEN-1) {
60 fragen[++aktFrage].anzeigen (MainActivity);
61 626364 65 }
66 }
6768}
6970}
die spiellogik
1package de.florian.spiel;
2
3import de.florian.spiel.R.id;
4import android.app.Activity;
5import android.widget.Button;
6import android.widget.TextView;
7
8
9class Frage {
10 private String frage;
11 private String option1;
12 private String option2;
13 private String option3;
14 private String option4;
15 private int loesung;
16
17 public Frage(String f, String o1, String o2, String o3, String o4, int l){
18 frage=f;
19 option1 = o1;
20 option2 = o2;
21 option3 = o3;
22 option4 = o4;
23 loesung = l;
24
25
26 }
27
28public void anzeigen(Activity MainActivity){
29((TextView) MainActivity.findViewById(id.frage)).setText(frage);
30((Button) MainActivity.findViewById(id.antwort1)).setText(option1);
31((Button) MainActivity.findViewById(id.antwort2)).setText(option2);
32((Button) MainActivity.findViewById(id.antwort3)).setText(option3);
33((Button) MainActivity.findViewById(id.antwort4)).setText(option4);
34
35}
36
37public boolean richtig(int ausgewaehlt) {
38 if(ausgewaehlt== this.loesung)
39 return true;
40 else
41 return false;
42}
43}
die fragen datei
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.