加载已保存的分数

时间:2014-08-01 17:36:23

标签: java android eclipse

我是新手。我正在创建一个应用程序,你可以在15秒内尽可能多地得分,然后它会保存你的分数并开始另一项活动,我必须在其中加载保存的分数并显示它而不是那个textview,但是我不知道该怎么做。这是第一个代码:

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);
    }
}

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

2 个答案:

答案 0 :(得分:2)

编写一个只传递几个字节的文件可能有点太多了。

您只需使用 SharedPreferences 即可。 (见here)。

或者如果您只想将分数传递给下一个活动,您可以使用:

/* pass extra data to next activity */
intent.putExtra("score", yourScore);

/* get passed data */
getIntent().getIntExtra("score", -1); // -1 will be returned if no score was passed

答案 1 :(得分:0)

在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);

}