【问题标题】:Disabling/Enabling a Continue button禁用/启用继续按钮
【发布时间】: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


    【解决方案1】:

    您在错误的活动上启用了按钮。以下是您的代码正在执行的操作,以大纲形式显示:

    1. onClick() 为 Activity #1 的 mContinueButton 运行
    2. Activity #1 的 mContinueButton 已启用
    3. 活动 #2 已启动
    4. Activity #1(连同它的 mContinueButton,它是此时唯一启用的按钮)在 Activity #2 甚至打开之前(finish() 调用的 b/c)被销毁。

    要解决此问题,请决定在 onResume() 中调用 setEnabled(true)。从onClick() 中删除setEnabled(true) 调用。例如:

    1. 禁用该按钮。
    2. 检查您的偏好布尔值。如果其中任何一个是错误的,请致电setEnabled(true)
    3. 设置按钮的onClick() 处理程序。这里不需要调用setEnabled()(b/c 已经做出决定),但保留 if/then 语句和所有其他代码,因为您仍然需要选择要打开的 Activity。李>

    请注意,您已将 onClick() 处理程序声明为 closure。当onResume() 执行时,该函数中的代码不会运行;它在用户单击启用的视图时运行。

    您的黄色框暂时不会造成任何问题;请参阅this question 获取解决方案。

    【讨论】:

    • 我开始理解这背后的逻辑。所以,就上面的代码而言,我必须这样写:“mContinueButton = (Button)findViewById(R.id.ContinueButton); mContinueButton.setEnabled(false); mContinueButton.setEnabled(true);”在 onResume() 下,并删除“mContinueButton.setEnabled(true);”来自 onClick() 的行?还是我也必须对其他活动进行更改?因为我按照你说的做了,但现在按钮总是启用的。另外, mContinueButton.setEnabled(false);出现在黄色框中:i.imgur.com/wfbjWPV.jpg
    • 我在答案中添加了更多细节。
    猜你喜欢
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 2015-09-02
    相关资源
    最近更新 更多