保存和加载得分 - android java(eclipse)

时间:2014-08-01 20:25:07

标签: java android eclipse

我在这里解释了所有内容loading saved score代码也在那里,请帮助我。 不要只发布一个部分,因为我真是太新了我不知道什么,如果你会回答,那就写下整件事,拜托!

老帖子: 我是新手。我正在制作一个应用程序,你可以在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() {
            // TODO Auto-generated method stub
            try {
            FileOutputStream outputStream = getApplicationContext().openFileOutput(filename, Context.MODE_PRIVATE);
            outputStream.write(scoreBytes);
            outputStream.close();
            } catch (Exception e) {
                  e.printStackTrace();
                }
            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);
        try {
            BufferedReader inputReader = new BufferedReader(new InputStreamReader(
                    openFileInput("filename")));
            String inputString;
            StringBuffer stringBuffer = new StringBuffer();                
            while ((inputString = inputReader.readLine()) != null) {
                stringBuffer.append(inputString + "\n");
            }
            textFinal.setText(stringBuffer.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

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

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

1 个答案:

答案 0 :(得分:0)

要将信息传递给其他活动,您可以查看Bundles。我会为你写完整件事,因为这是一个很好的做法,但基本上你得到的是一个好的开始。