在另一个活动中显示分数结果

时间:2015-01-03 19:14:37

标签: android textview

enter image description here 如何在其他活动的活动中显示分数结果?我在这里有我的MainActivity和ScoreActivity。我想在MainActivity的TextView中显示ScoreActivity中的分数。可能吗?我怎样才能做到这一点?

MainActivity

//COUNTDOOWNTIMER
public void onFinish() {
			// TODO Auto-generated method stub
			timer.setText("Time's Up!");
			topScore.setText(score.getText());//the textview for best score
			run();//to store the value in topScore
			
			Intent i = new Intent("app.thesis.boggleit.TOPSCORE");
			startActivity(i); //themedActivity
			
			Intent score = new Intent(MainActivity.this, TopScore.class);
			score.putExtra("scores", topScore.getText().toString());
			
			}

最高得分

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class TopScore extends Activity{

	TextView topscore;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.top_score);
		
		topscore = (TextView) findViewById(R.id.tvNewTScore);
		
		String scores = getIntent().getStringExtra("scores");
		topscore.setText(scores);
		
	}

}

2 个答案:

答案 0 :(得分:1)

在主要活动中:

public void onFinish() {
        // TODO Auto-generated method stub
        timer.setText("Time's Up!");
        topScore.setText(score.getText());//the textview for best score
        run();//to store the value in topScore

        Intent i = new Intent(MainActivity.this,ScoreActivity.class);
        i.putExtra("scores", topScore.getText().toString());

        startActivity(i); //themedActivity
}

得分:

String scores = getIntent().getStringExtra("scores");

答案 1 :(得分:1)

在您的意向消息对象中,添加一个包并保存您的分数值。

在“分数活动”中,从分组中检索值。

在MainActivity中:

Intent intent = new Intent();
intent.setClass(this, Score_Activity.class);
intent.putExtra("EXTRA_ID", "Score Value");
startActivity(intent);

在Score_Activity中:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Bundle extras = getIntent().getExtras();
  if (extras != null) {
   String datas= extras.getString("EXTRA_ID");
   if (datas!= null) {
        // do stuff
   }        
}