How to keep score in the game and answer questions?

I created a game with a question and answer, and I need to keep score without creating any database. I created an individual activity and .xml for each question, and the user needs to enter the answer in the text box, and if the answer is correct, he automatically proceeds to the next action (i.e. the next question). Now I need to keep the score so that after the correct answer from the user, the score should be displayed in the upper right corner of the screen. How can I do it? please help. Here's the java activity and xml file for my second level one question:

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 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 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 file:

<?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>
+1
source share
3 answers

, , , - : Java?

, : Java:

, , . :

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();
+3

, .

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 not the correct answer
        ....
        // -1 point per incorrect answer
        score --;
    }
}

, , , SharedPreferences.

+2

If you want the ratings to be kept all the time, please use SharedPreferences, otherwise, if you want this to be only in the working state of the application, use the static data element in the class to store the ratings

0
source

All Articles