一尘不染

载入保存的分数

java

我是新手。我正在制作一个应用,其中您有15秒的得分,然后它会保存您的得分并启动另一个活动,在该活动中,我必须加载已保存的得分并显示该得分,而不是显示该文本视图,但是我不知道该怎么做。这是第一个代码:

package com.cannongaming.supertouch;

import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.os.CountDownTimer;

public class StartActivity<TextView> extends ActionBarActivity implements OnClickListener{
    Button buttonTap, buttonLet;
    TextView textScore, textTime;
    int score=0;
    byte[] scoreBytes = new byte[3];
    String filename = "myscore";
    int timer = 15000; // 1000 = 1 second

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

        //1
        buttonTap = (Button)findViewById(R.id.buttonTap);
        buttonLet = (Button)findViewById(R.id.buttonLet);
        textScore = (TextView)findViewById(R.id.textScore);
        textTime = (TextView)findViewById(R.id.textTime);
        scoreBytes[0] = (byte) score;
        scoreBytes[1] = (byte) (score >> 8);
        scoreBytes[2] = (byte) (score >> 16);
        ((android.widget.TextView) textTime).setText("00:00:15");

        //2
        ((android.widget.TextView) textScore).setText(String.valueOf(score));

        //3
        buttonTap.setOnClickListener(this);

    buttonLet.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {

        final CounterClass timer = new CounterClass(15000, 1000);
        timer.start();
        score++;
        ((android.widget.TextView) textScore).setText(String.valueOf(score));
        view.setVisibility(View.GONE);
    }});
    }
    public void onClick(View src){
        switch(src.getId()){
        case R.id.buttonTap:
            score++;
            ((android.widget.TextView) textScore).setText(String.valueOf(score));
            break;
        }
    }

    public class CounterClass extends CountDownTimer {

        public CounterClass(long millisInFuture, long countDownInterval){
            super(millisInFuture, countDownInterval);
            // TODO Auto-generated constructor stub
        }

        public void onTick(long millisUntilFinished){

            long millis = millisUntilFinished;
            String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
                    TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
            System.out.println(hms);
            ((android.widget.TextView) textTime).setText(hms);
        }

         @Override
public void onFinish() {
    getSharedPreferences().edit().putInt("SOME_IDENTIFIER", score).commit();
    Intent i = new Intent(getApplicationContext(), ScoreActivity.class);
    startActivity(i);
}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.start, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这是在第一个之后开始的另一个:

package com.cannongaming.supertouch;

import java.io.BufferedReader;

public class ScoreActivity extends ActionBarActivity {
    Button buttonOk;
    TextView textFinal;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);
        textFinal = (TextView)findViewById(R.id.textFinal);
int defaultValue = 0;
int score = getSharedPreferences().getInt("SOME_IDENTIFIER", defaultValue);
textFinal.setText(score);

    }

    @Override
    public void onBackPressed() {
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.score, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

编辑:我什至不确定它是否真的保存在第一个,所以请帮助我。


阅读 249

收藏
2020-12-03

共1个答案

一尘不染

在StartActivity方法中覆盖的onFinish中,只需执行以下操作:

    @Override
    public void onFinish() {
        getSharedPreferences("APP", MODE_PRIVATE).edit().putInt("SOME_IDENTIFIER", score).commit();
        Intent i = new Intent(getApplicationContext(), ScoreActivity.class);
        startActivity(i);
    }

然后在ScoreActivity的onCreate方法上:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_score);
    textFinal = (TextView)findViewById(R.id.textFinal);
    int defaultValue = 0;
    int score = getSharedPreferences("APP", MODE_PRIVATE).getInt("SOME_IDENTIFIER", defaultValue);
    textFinal.setText(score);

}
2020-12-03