装载保存的得分 [英] loading saved score
问题描述
我是新手。我在做一个应用程序中,你有15秒钟的进球一样多,你可以,然后将其保存你的分数,并开始另一项活动中,我要让它负载保存的得分和表明的TextView它代替,但我不知道该怎么做。这是第一个code:
I am a newbie. I'm making an app in which you have 15 seconds to score as much as you can, and then it saves your score and starts another activity in which I have to make it load that saved score and show it instead of that textview, but I don't know how to do that. Here is the first code:
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);
}
}
下面是第一个接一个年代就开始了另一个问题:
Here is another one that's started after the first one:
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);
}
}
编辑:我也不知道,如果它实际上是保存在第一位的,所以请大家帮我
I'm not even sure if it's actually saved in the first one, so please help me.
推荐答案
在您的onFinish覆写上StartActivity方法只是做:
In your onFinish overriden on StartActivity method just do:
@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方法:
And then on ScoreActivity onCreate method:
@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);
}
这篇关于装载保存的得分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!