即时创建一个具有高分的游戏,但我的问题是我无法在我的levelcomplete.class的按钮中创建一个settext到我的highscore.class中。我的观点是,如果从我的levelcomplete.class中点击我的高分(按钮),它将在我的highscore.class上自动显示textview settext
我的解释流程levelcomplete.class = highscore(button)= highscore.class = settext 10/10
就像将分数保存为高分
levelcomplete.class
public class levelcomplete extends Activity {
Button highscore;
int highestScore;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TODO Auto-generated method stub
highscore = (Button) findViewById(R.id.save);
highscore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Pass your score to other activity through Android's intent.
Intent intent = new Intent(getApplicationContext(),
highscore.class);
//THis highestScore variable will actually hold the score you get in this activity.
intent.putExtra("score", highestScore);
startActivity(intent);
}
});
}
}
highscore.class
public class highscore extends Activity {
TextView score;
Button back;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highscore);
score = (TextView) findViewById(R.id.score);
int highestScore = -1;
//Now use this score variable to set anywhere.
Bundle extras = getIntent().getExtras();
if (extras != null) {
highestScore = extras.getInt("score");
}
back = (Button) findViewById(R.id.btn_back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Back",
Toast.LENGTH_SHORT).show();
}
});
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
有3种方法可以做到这一点:
第一种方式:(不推荐):
public static int HighScore;
在highscore.class上创建此静态变量,您将在levelcomplete.class上设置此公共变量,然后您可以使用此变量。但这种方法不是一个好方法。因为如果您的设备必须运行大量应用程序,则此静态变量将被垃圾回收删除。我们的数据将是云数据:)
第二种方式:(对于这种最适合你的煽动)
使用Bundles在活动之间传递数据。这是您的需求的最佳选择。 发送方:
intent.putExtra("OUR_SCORE_TAG", INTEGER_SCORE);
Bundle e = getIntent().getExtras();
if (e!= null) {
ourSCORE = extras.getInt("OUR_SCORE_TAG");
}
第三种方式:(最佳方式但未针对您的示例进行重新排列,这可能会使用更复杂的类型):
在你的levelcomplete.class中,
public interface MyHighScoreClickListener
{
public void onMyScoreButtonClickListener(int p_score);
}
并在levelcomplete.class中创建此接口的实例,例如;
public MyHighScoreClickListener listener;
highscore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Pass your score to other activity through Android's intent.
Intent intent = new Intent(getApplicationContext(),
highscore.class);
//THis highestScore variable will actually hold the score you get in this activity.
intent.putExtra("score", highestScore);
startActivity(intent);
listener.onMyScoreButtonClickListener(highestScore); // This is deadline :)
}
});
你的highscore.class必须是ipmlement MyHighScoreClickListener,如果你不这样做,这个patternd不会运行。
public class highscore extends Activity implements MyHighScoreClickListener ...
然后覆盖我们的highscore.class中的onMyScoreButtonClickListener方法,并编写将设置文本的代码。像那样实施:
public class highscore extends Activity {
TextView score;
Button back;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highscore);
score = (TextView) findViewById(R.id.score);
int highestScore = -1;
@Override
public void MyHighScoreClickListener(int p_score)
{
this.highestScore = p_score;
score.setText(Integer.toString(this.hightScore));
}
这是一种设计模式。您不必在此示例中使用此模式,但这将有所帮助。
答案 1 :(得分:0)
替换您的代码:
Bundle extras = getIntent().getExtras();
if (extras != null) {
highestScore = extras.getInt("score");
}
使用此代码获取您的高分整数:
getIntent().getIntExtra("score", 0);
它可能对你有帮助。