2016-08-29 11 views
0

私はAndroid Studio/Javaについては初心者ですので、質問が愚かであれば私を許してください。続行ボタンを無効にする/有効にする

私は単純な謎のゲームである私の最初のアプリをほぼ完成させました。質問が尋ねられており、適切な答えであなたは次の活動に行きます。 メインメニューには、新しいゲームと続行の2つのオプションがあります。 [続行]ボタンは、共有設定を使用してチャームのように機能します。押すと、最後に回答しなかった質問に移動します。

私の問題は次のとおりです。最初の質問に答えると、[続行]ボタンをデフォルトで無効にすることはできません。まあ、真実は、私は

@Override 
    public void onResume() { 
    super.onResume(); 
    setContentView(R.layout.activity_main_menu_with_logo); 

    mContinueButton = (Button)findViewById(R.id.ContinueButton); 
    mContinueButton.setEnabled(false); 
    ... 
    } 

を使用して、それを無効にすることができますですが、私はそれを後で有効にする方法を発見していません。私は、「公共のボイドのonClick」の下にある

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

を書き込むことによって、それを動作させるだろうと思ったが、いや、それはしていません。質問に答えても、私はまだそれを押すことができません。ですから、私は何を修正すべきですか?どんな種類の助けでも大歓迎です。

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); 
} 
} 

答えて

0

あなたは間違った活動上のボタンを有効にする:

はここで完全なスクリプトです。ここではアウトライン形式で、あなたのコードが何をしているかである:第1位のmContinueButtonが

  • アクティビティ#2を有効になっている活動#1のmContinueButton
  • 活動が
  • 活動#1を起動するため

    1. のonClick()を実行します(この時点で唯一有効になっているmContinueButtonとともに)アクティビティ#2がfinish()コールのb/cを開く前に破棄されます。

    修正するには、onResume()でsetEnabled(true)を呼び出すことを決定してください。 onClick()からsetEnabled(true)の通話を削除します。例:

    1. ボタンを無効にします。
    2. ブーリアンを確認してください。いずれかが偽の場合は、setEnabled(true)に電話してください。
    3. ボタンのonClick()ハンドラを設定します。ここではsetEnabled()への呼び出しは必要ありませんが、if/thenステートメントとそこにあるすべてのコードを保持しておく必要があります。なぜなら、まだ開くべきアクティビティを選択する必要があるからです。

    onClick()ハンドラをclosureと宣言していることに注意してください。その関数内のコードはonResume()が実行されたときには実行されません。ユーザーが有効なビューをクリックすると実行されます。

    あなたの黄色いボックスは現時点では問題ありません。ソリューションについてはthis questionを参照してください。

  • +0

    私はこれの背後にある論理を理解し始めます。上記のコードでは、次のように記述する必要があります。 "mContinueButton =(Button)findViewById(R.id.ContinueButton); mContinueButton.setEnabled(false); mContinueButton.setEnabled(真); " onResume()の下で を削除し、 "mContinueButton.setEnabled(true);"を削除します。 onClick()からの行?あるいは他の活動にも変更を加える必要がありますか?なぜなら、あなたが言ったことをやったからです。しかし今、ボタンは常に有効になっています。 また、mContinueButton.setEnabled(false);黄色のボックスに表示されます:http://i.imgur.com/wfbjWPV.jpg – Animated24

    +0

    私は答えに詳細を追加しました。 – greeble31

    関連する問題