我创建了一个问答游戏,我需要在不创建任何数据库的情况下保持得分。我为每个问题创建了一个单独的活动和.xml,用户需要在文本字段中输入答案,如果答案是正确的,它会自动转到下一个活动(即下一个问题)。现在,我需要保持得分,以便在用户输入正确的答案后,分数应显示在屏幕的右上角。我怎么做?请帮忙。这是第一级第二个问题的java活动和xml文件:
package com.golo.user.gaunkhanekatha;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Handler;
public class TwoActivity extends Activity {
public Button check;
public EditText typeh;
private Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
toast = Toast.makeText(TwoActivity.this, "", Toast.LENGTH_SHORT);
check = (Button)findViewById(R.id.check1); //R.id.button is the id on your xml
typeh = (EditText)findViewById(R.id.type1); //this is the EditText id
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//Here you must get the text on your EditText
String Answer = (String) typeh.getText().toString(); //here you have the text typed by the user
//You can make an if statement to check if it's correct or not
if(Answer.equals("piano") || (Answer.equals("keyboard")))
{
///Correct Toast
toast.setText("Correct! Now, next question...");
toast.setGravity(Gravity.TOP | Gravity.LEFT, 500, 300);
toast.show();
Intent i = new Intent(TwoActivity.this, ThreeActivity.class);
startActivity(i);
finish();
}
else{
//It's not the correct answer
toast.setText("Wrong! Try Again!");
toast.setGravity(Gravity.TOP | Gravity.LEFT, 500, 300);
toast.show();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if(toast!= null) {
toast.cancel();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_aboutus, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:weightSum="1"
android:textAlignment="center"
android:id="@+id/level1">
<TextView
android:layout_width="300dp"
android:layout_height="200dp"
android:text="What has 88 keys but cannot open a single door?"
android:id="@+id/que1"
android:width="255dp"
android:textSize="30dp"
android:layout_margin="50dp"
android:textStyle="italic"
android:gravity="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/type1"
android:layout_gravity="center_horizontal"
android:hint="Type here..." />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check answer..."
android:id="@+id/check1"
android:layout_gravity="center_horizontal" />
</LinearLayout>
答案 0 :(得分:3)
如果您希望在两次运行之间保持分数,您可以使用以下内容将分数写入文本文件: How do I create a file and write to it in Java?
然后在想要显示分数时从中读取: Java: How to read a text file
或者,如果您只想在应用程序运行期间保持分数,则可以使用分数的单个分数。如:
public class Score {
private static int score = 0;
//So that this class can't be instantiated
private Score () {
}
//Return current score
public static int getScore() {
return score;
}
//Increment score
public static void increaseScoreByOne() {
score = score++;
}
//Decrement score
public static void decreaseScoreByOne() {
score = score--;
}
//Reset score back to 0
public static void resetScore() {
score = 0;
}
}
然后可以通过调用:
在应用程序的任何位置访问它Score.getScore();
Score.increaseScoreByOne();
Score.decreaseScoreByOne();
Score.resetScore();
答案 1 :(得分:2)
如果你不需要在戏剧之间保持得分,只需在课堂上添加一个新属性,并在课堂流程中增加它。
public class TwoActivity extends Activity {
public Button check;
public EditText typeh;
private Toast toast;
private int score;
if(Answer.equals("piano") || (Answer.equals("keyboard")))
{
///Correct Toast
....
// +5 points per correct answer
score +=5;
} else{
//It's not the correct answer
....
// -1 point per incorrect answer
score --;
}
}
如果您想在应用程序执行之间记分,或者可能显示排名,则需要使用SharedPreferences
。查看this question示例
答案 2 :(得分:0)
如果您希望始终保存分数,请使用SharedPreferences,否则如果您希望它仅处于应用程序的运行状态,则使用类中的静态数据成员来存储分数