Java 如何在Android中保存数据?

Java 如何在Android中保存数据?,java,android,xml,Java,Android,Xml,因此,我制作了一款Cookie Clicker应用程序,帮助我开始学习如何使用android Studio在android上编程。 但是我需要一种方法来保存玩家拥有的饼干数量,这样当他退出游戏并回来时,这些饼干就会被保存。 这是我目前拥有的代码: MainActivity.java: package com.android.example; import android.media.MediaPlayer; import android.os.Bundle; import android.su

因此,我制作了一款Cookie Clicker应用程序,帮助我开始学习如何使用android Studio在android上编程。
但是我需要一种方法来保存玩家拥有的饼干数量,这样当他退出游戏并回来时,这些饼干就会被保存。
这是我目前拥有的代码:

MainActivity.java:

package com.android.example;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            /* Sound */
        final MediaPlayer cookieSound = MediaPlayer.create(this, R.raw.cookie_eat_short);

        ImageButton playCookieSound = (ImageButton) this.findViewById(R.id.cookie_button);

        playCookieSound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cookieSound.start();
                /* End of Sound */

                /* The following lines add 1 to the score and then display it. */
                scoreCookies++;
                display(scoreCookies);
            }

        });

    }

    /*  Initializing the score to 0
    * This is also what I want to change, make it set the score to the saved score.*/
    int scoreCookies = 0;

    /* This method displays the score on the screen. */
    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.score_text_view);
        quantityTextView.setText("" + number);
    }
}
activity_main.xml:

<ImageButton
    android:id="@+id/cookie_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="124dp"
    android:background="@android:color/transparent"
    android:scaleType="fitStart"
    app:srcCompat="@drawable/cookiepic" />

<TextView
    android:id="@+id/score_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="52dp"
    android:text="0"
    android:textSize="42sp"
    android:textAppearance="@style/TextAppearance.AppCompat.Headline" />


提前感谢您的帮助:)

您可以使用共享首选项保存少量数据

写入

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
阅读

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

您可以使用共享首选项保存少量数据

写入

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
阅读

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

请看看谷歌或其他搜索引擎。至少有50000篇关于它的文章。你应该阅读这篇文章,请看看谷歌或其他搜索引擎。至少有50000篇关于它的文章,你应该读一下