Ich bin ein Anfänger und ich versuche ein Quiz-Spiel ohne Datenbank und mehrere Aktivitäten zu machen. Ich möchte das Scoring-System als Textansicht in allen Aktivitäten erhalten und es sollte sich nach richtiger oder falscher Antwort ändern. Ich versuche seit über einer Woche, es herauszufinden, und ich denke nur, dass ich diese Zeit verschwendet habe. Hier ist ein Beispielcode. Alle Hilfe wird geschätzt.Wie man einen TextView mit inkrementellen Punkten in mehreren Aktivitäten erstellt (Quiz-Spiel ohne Datenbank)
mein Page4.java
public class Page4 extends AppCompatActivity {
int score = 0;
TextView scored1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);;
setContentView(R.layout.activity_page4);
scored1 = (TextView) findViewById(R.id.score);
scored1.setText(String.valueOf(score));
}
public void nocircle(View view){
score = score + 5;
scored1.setText(String.valueOf(score));
Intent intent = new Intent(this,Page5.class);
intent.putExtra("score",String.valueOf(score));
startActivity(intent);
ActivityOptions options = ActivityOptions.makeScaleUpAnimation(view, 0,
0, view.getWidth(), view.getHeight());
startActivity(intent, options.toBundle());
}
}
activity_page4
<LinearLayout
android:layout_width="50dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#05083e"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:id="@+id/tvLabelScore"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#f6f6f6"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/score"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#e5e5e5"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<TextView
android:layout_width="180dp"
android:layout_height="100dp"
android:layout_below="@+id/imageView10"
android:text="NO"
android:gravity="center"
android:fontFamily="casual"
android:textSize="70sp"
android:textColor="#fff"
android:layout_toRightOf="@+id/answeryes"
android:onClick="nocircle"
/>
</RelativeLayout>
Wenn die neue Aktivität Page5.class beginnt, die Textview 5 ist ... aber wenn ich auf die Schaltfläche klicken die Partitur wird zurückgesetzt und Es werden nur 5 Punkte hinzugefügt. Ich will es so, dass die Punkte die Page4.java-Punktzahl und diese Page5.java-Punktzahl hinzufügen und in der Textansicht anzeigen .... und mit der gleichen für die nächste Aktivität fortfahren.
Page5.java Code
public class Page5 extends AppCompatActivity {
int score = 5;
TextView scored;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);;
setContentView(R.layout.activity_page5);
scored = (TextView) findViewById(R.id.score1);
Intent mintent = getIntent();
String score = mintent.getStringExtra("score");
scored.setText(String.valueOf(score));
}
public void rightanswer(View view){
scored.setText(String.valueOf(score));
Intent intent = new Intent(this, Page6.class);
intent.putExtra("ta_score",String.valueOf(score));
startActivity(intent);
ActivityOptions options = ActivityOptions.makeScaleUpAnimation(view, 0,
0, view.getWidth(), view.getHeight());
startActivity(intent, options.toBundle());
}
}
und meine activity_page5 schließlich
<LinearLayout
android:layout_width="50dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#05083e"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:id="@+id/tvLabelScore"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#f6f6f6"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/score1"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#e5e5e5"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="rightanswer"
/>
Jede Hilfe awesomely mit freundlichen Worten des Lobes geschätzt würde.
ich ein 'static' Variable verwenden würde in eine Klasse. Damit ich diesen Wert von überall in meiner App ändern konnte. –