我在这里有一些有趣的问题,如果游戏重新启动/重新打开,如何返回最新活动,因为我有一个新的游戏按钮并继续按钮。所以当点击继续按钮时,它将返回到上次打开的活动之前和条件是活动是从activityone到activityfive的随机
我将用我的代码解释
这是menu.class
public class menu extends Activity {
int level;
Button newgame, continues, continuelocked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
continuelocked=(Button)findViewById(R.id.buttoncontinuelocked);
continues=(Button)findViewById(R.id.buttoncontinue);
newgame=(Button)findViewById(R.id.buttonnewgame);
newgame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
Intent i =new Intent(menu.this, intro.class);
startActivity(i);
}
});
}
public void onResume() {
super.onResume();
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
level = pref.getInt("Level", 0);
if(level == 0)
{
continuelocked.setVisibility(View.VISIBLE);
continues.setVisibility(View.GONE);
}
if(level == 1)
{
continuelocked.setVisibility(View.GONE);
continues.setVisibility(View.VISIBLE);
}
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Level", level);
editor.commit();
continues=(Button)findViewById(R.id.buttoncontinue);
continues.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
//How to set this method to return to latest activity that i play before
//if i use random levelactivity?
});
}
@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);
}
}
并在intro.class中我使用此方法使活动随机, 在这里查看我的代码 -
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
button5 = (Button)findViewById(R.id.button5);
if(v==button5) {
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Level", 1);
editor.commit();
// Here, we are generating a random number
Random generator = new Random();
int number = generator.nextInt(5) + 1;
// The '5' is the number of activities
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
case 1:
// E.g., if the output is 1, the activity we will open is ActivityOne.class
activity = ActivityOne.class;
break;
case 2:
activity = ActivityTwo.class;
break;
case 3:
activity = ActivityThree.class;
break;
case 4:
activity = ActivityFour.class;
break;
default:
activity = ActivityFive.class;
break;
}
// We use intents to start activities
Intent intent = new Intent(getBaseContext(), activity);
startActivity(intent);
}
在每个活动“一到五”中我都放了相同的随机活动代码
@Override
public void onClick(View v) {
// Here, we are generating a random number
Random generator = new Random();
int number = generator.nextInt(5) + 1;
// The '5' is the number of activities
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
case 1:
// E.g., if the output is 1, the activity we will open is ActivityOne.class
activity = ActivityOne.class;
break;
case 2:
activity = ActivityTwo.class;
break;
case 3:
activity = ActivityThree.class;
break;
case 4:
activity = ActivityFour.class;
break;
default:
activity = ActivityFive.class;
break;
}
// We use intents to start activities
Intent intent = new Intent(getBaseContext(), activity);
startActivity(intent);
}
}
所以我的问题是
首先。如果活动是随机的,如何使用“继续”按钮打开最后一个活动?
二。如果在每个Activity中都有一个相同的随机代码,一个到五个,如何将Disabled设置为之前已打开的Activity?
任何人都能解释一下吗?
已更新
我找到了第二个答案的解决方案,但我还没试过,所以我不知道它是否有效
所以我改变了这样的代码
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
button5 = (Button)findViewById(R.id.button5);
if(v==button5) {
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Level", 1);
editor.commit();
layout7.setVisibility(View.GONE);
layout7.setVisibility(View.VISIBLE);
// Here, we are generating a random number
Random generator = new Random();
number = generator.nextInt(5) + 1;
// The '5' is the number of activities
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
// E.g., if the output is 1, the activity we will open is ActivityOne.class
case 1: if(one == 1){
activity = ActivityOne.class;
}
else if(one == 2){
Random generatorone = new Random();
number = generatorone.nextInt(5) + 1;
}
break;
case 2: if(two== 1){
activity = ActivityTwo.class;
}
else if(two== 2){
Random generatortwo = new Random();
number = generatortwo.nextInt(5) + 1;
}
break;
case 3:if(three== 1){
activity = ActivityThree.class;
}
else if(three== 2){
Random generatorthree = new Random();
number = generatorthree.nextInt(5) + 1;
}
break;
case 4:if(four == 1){
activity = ActivityFour.class;
}
else if(four == 2){
Random generatorFour = new Random();
number = generatorFour.nextInt(5) + 1;
}
break;
default:if(five== 1){
activity = ActivityFive.class;
}
else if(five== 2){
Random generatorfive = new Random();
number = generatorfive.nextInt(5) + 1;
}
break;
}
// We use intents to start activities
Intent intent = new Intent(getBaseContext(), activity);
startActivity(intent);
}
};
我认为,如果int是show == 2,则表示Activity之前已经打开过。所以它会再次随机,直到找到== 1
的活动任何人都可以更正上面的代码吗?是对还是不对?
我的第一个问题仍然没有答案
首先。如果活动是随机的并且应用程序重新打开/重新启动,如何使用“继续”按钮打开上一个活动?
提前谢谢你,祝你有个美好的一天