禁用/启用“继续”按钮

时间:2016-08-29 18:49:35

标签: java android button sharedpreferences

我是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();

在"公共无效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);
}
}

1 个答案:

答案 0 :(得分:0)

您正在启用错误活动的按钮。以下是您的代码正在执行的操作:

  1. onClick()针对Activity#1的mContinueButton
  2. 运行
  3. 活动#1的mContinueButton已启用
  4. 活动#2已启动
  5. 活动#1(连同它的mContinueButton,这是此时唯一启用的)在活动#2打开之前销毁,终结()调用b / c。
  6. 要修复,请决定在onResume()中调用setEnabled(true)。从setEnabled(true)移除onClick()来电。例如:

    1. 禁用按钮。
    2. 检查你的偏好布尔。如果其中任何一个为假,请致电setEnabled(true)
    3. 设置按钮的onClick()处理程序。此处不需要调用setEnabled()(b / c已经做出决定),但保留if / then语句以及其他所有代码,因为您仍需要选择要打开的Activity。
    4. 请注意,您已将onClick()处理程序声明为closureonResume()执行时,该函数中的代码不会运行;它在用户单击已启用的视图时运行。

      你的黄色盒子此刻不会造成任何问题;请参阅this question了解解决方案。

相关问题