Clicker App:无法正确保存点击次数

时间:2016-09-10 19:36:47

标签: android

我制作了一个Clicker应用程序(如cookie clicker)。但是当我重新启动应用程序时,应用程序会保存点击次数,但只能看到,所以我的意思是当我点击按钮时,点击次数将再次设置为0。所以我的问题是:我的SharedPreference是正确的,还是我做错了? 我的完整MainActivity.java:

package com.rage.clicker;

import android.content.Context; 
import android.content.SharedPreferences; 
import android.media.AudioManager;
import android.media.SoundPool; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle;
import android.view.View; import android.view.animation.Animation;
import android.view.animation.AnimationUtils; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast;
import org.w3c.dom.Text;

import static com.rage.clicker.R.id.highscore_text; 
import static com.rage.clicker.R.id.textView88;

public class MainActivity extends AppCompatActivity {
    SoundPool mySound;
    int playClick;
    private final String TAG = this.getClass().getName();
    ImageView hi;
    TextView tv_clicks;
    TextView ht;
    ImageView b_click;
    Button save;
    int clicks;
    SharedPreferences sf;
    public static final String preference = "pref";
    public static final String saveIt = "saveKey";
    private TextView highScoreView;
    private TextView currentScoreView;
    private int currentScore;
    private int highScore;
    private String highScoreString;
    private String currentScoreString;
    private SharedPreferences msharedPreferences;

    private static final String FILENAME = "PreferencesFilename";
    private static final String VAL_KEY = "ValueKey";
    private EditText editText;
    private TextView TEV; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Speichern laden
        TEV = (TextView) findViewById(R.id.textView88);
        SharedPreferences sharedPrefs = getSharedPreferences(FILENAME, 0);
        TEV.setText(sharedPrefs.getString(VAL_KEY, ""));
        //Speichern laden
        msharedPreferences = getPreferences(MODE_PRIVATE);
        mySound = new SoundPool(100, AudioManager.STREAM_MUSIC, 0);
        playClick = mySound.load(this, R.raw.click, 1);
        tv_clicks = (TextView) findViewById(textView88);
        b_click = (ImageView) findViewById(R.id.imageView);
        hi = (ImageView) findViewById(R.id.imageView);
        b_click = (ImageView) findViewById(R.id.imageView);
        ht = (TextView) findViewById(highscore_text);
        final TextView TV = (TextView) findViewById(R.id.highscore_text);
        currentScoreString = getString(R.string.current_score);
        highScoreString = getString(R.string.high_score);
        currentScore = clicks;
        highScore = msharedPreferences.getInt(highScoreString, 0);

        save = (Button) findViewById(R.id.button);
        save.setOnClickListener( 
            new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast dumplings= Toast.makeText(MainActivity.this, "Adolf4", Toast.LENGTH_LONG);
                dumplings.show();
            }
        });

        b_click.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                final ImageView zoom = (ImageView) findViewById(R.id.imageView);
                final Animation zoomAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.size);
                zoom.startAnimation(zoomAnimation);
                mySound.play(playClick, 1, 1, 1, 0, 1);
                clicks++;

                tv_clicks.setText("Klicks: " + clicks);
                ht.setText(currentScoreString + ": Test" + currentScore); 
            }
        });
    }

    protected void init() {
        currentScoreView.setText(currentScoreString + ": asd" + currentScore);
        highScoreView.setText(highScoreString + ":34 " + highScore);
    }

    @Override
    protected void onStop() {
        super.onStop(); 
        //Speichern
        SharedPreferences sharedPrefs = getSharedPreferences(FILENAME, 0);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putString(VAL_KEY, TEV.getText().toString());
        editor.commit();
        //Speichern
    } 
}

1 个答案:

答案 0 :(得分:1)

当您保存数据时,您会从TEV TextView获取点击次数,但在onClick次内,您更新tv_clicks并且TEV始终保持为零默认。您应该像这样保存点击价值

editor.putInt(VAL_KEY, clicks);

然后您应加载点击次数值并将其设置在TextView

clicks = sharedPrefs.getInt(VAL_KEY, 0)
TEV.setText(String.valueOf(clicks));