【发布时间】:2016-08-29 18:49:35
【问题描述】:
我是 Android Studio/Java 的初学者,所以如果问题有点愚蠢,请原谅我。
我几乎完成了我的第一个应用程序,这是一个简单的谜语游戏;有人问了一个问题,有了正确的答案,你就可以进入下一个活动 - 等等。 在主菜单中,和往常一样有两个选项:新游戏和继续。继续按钮的作用就像使用共享偏好设置的魅力 - 按下时,它会将您带到您没有回答的最后一个问题。
我的问题是:我不能默认禁用继续按钮,然后在回答第一个问题时启用。好吧,事实是我可以使用
禁用它 @Override
public void onResume() {
super.onResume();
setContentView(R.layout.activity_main_menu_with_logo);
mContinueButton = (Button)findViewById(R.id.ContinueButton);
mContinueButton.setEnabled(false);
...
}
但我还没有找到稍后启用它的方法。我认为通过写作
if (!(question00Answered)) {
mContinueButton.setEnabled(true);
Intent intent = new Intent(MainMenuWithLogo.this, Question00.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
在“public void onClick”下可以使它工作,但不,它没有。即使问题得到了回答,我仍然无法按下它。那么,关于我应该修复什么的任何提示?任何形式的帮助将不胜感激。
这是完整的脚本:
MainMenuWithLogo.Java
public class MainMenuWithLogo extends AppCompatActivity {
private Button mStartInterrogationButton;
private VideoView mLogoprwto;
private Button mContinueButton;
MediaPlayer song;
@Override
protected void onPause() {
super.onPause();
song.release();
}
@Override
public void onResume() {
super.onResume();
setContentView(R.layout.activity_main_menu_with_logo);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
song = MediaPlayer.create(this, R.raw.chopin);
song.start();
song.setLooping(true);
mLogoprwto = (VideoView) findViewById(R.id.logoprwto);
mLogoprwto.setVideoPath("android.resource://its_destination/"+R.raw.teloslogo);
mLogoprwto.start();
mLogoprwto.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mLogoprwto.start();
}
});
mStartInterrogationButton = (Button)findViewById(R.id.StartInterrogationButton);
mStartInterrogationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startGame();
}
});
mContinueButton = (Button)findViewById(R.id.ContinueButton);
mContinueButton.setEnabled(false);
mContinueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences prefs = getSharedPreferences("Stage", MODE_PRIVATE);
boolean question00Answered = prefs.getBoolean("QuestionZero", false);
boolean question01Answered = prefs.getBoolean("QuestionOne", false);
boolean question02Answered = prefs.getBoolean("QuestionTwo", false);
if (!(question00Answered)) {
mContinueButton.setEnabled(true);
Intent intent = new Intent(MainMenuWithLogo.this, QuestionZero.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
} else if (!(question01Answered)) {
mContinueButton.setEnabled(true);
Intent intent = new Intent(MainMenuWithLogo.this, QuestionOne.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
} else if (!(question02Answered)) {
mContinueButton.setEnabled(true);
Intent intent = new Intent(MainMenuWithLogo.this, QuestionTwo.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}else {
mContinueButton.setEnabled(true);
Intent intent = new Intent(MainMenuWithLogo.this, End.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
}
});
}
private void startGame () {
Intent intent = new Intent(this, Intro.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
@Override
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
【问题讨论】:
标签: java android button sharedpreferences