2016-08-29 2 views
0

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

그러나 나중에 사용할 수 있도록 설정할 방법을 찾지 못했습니다. 나는 그것을 "public void 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. 의 onclick()는 활동 # 1의 mContinueButton
  2. 1의 mContinueButton가 활성화되어 #
  3. 활동
  4. 활동 # 2가 시작
  5. 활동 # 1 실행 (이 시점에서 유일하게 활성화 된 mContinueButton과 함께) 활동 # 2가 끝나기도 전에 finish() 호출의 b/c가 파괴됩니다.

수정하려면 onResume()에서 setEnabled(true)으로 전화를 걸어 결정하십시오. onClick()에서 setEnabled(true) 통화를 삭제하십시오. 예 :

  1. 버튼을 비활성화하십시오.
  2. 환경 설정 불리언을 검사하십시오. 그 중 하나라도 거짓이면 setEnabled(true)으로 전화하십시오.
  3. 단추의 onClick() 처리기를 설정하십시오. 여기에는 setEnabled()에 대한 호출이 필요하지 않지만 (b/c 결정이 이미 내려져 있음) if/then 문과 다른 모든 코드는 그대로 유지해야합니다. 왜냐하면 열려고하는 활동을 선택해야하기 때문입니다.

onClick() 처리기를 closure으로 선언했음을 유의하십시오. 이 함수의 코드는 onResume()이 실행될 때 실행되지 않습니다. 사용자가 사용 가능한보기를 클릭하면 실행됩니다.

노란색 상자는 현재 문제를 일으키지 않습니다. 해결 방법은 this question을 참조하십시오.

+0

나는이 논리를 이해하기 시작했다. 그래서, 지금까지 위의 코드에 관한 한,이처럼 아래로 작성해야합니다 : "mContinueButton = (버튼) findViewById를 (R.id.ContinueButton) (false)를 mContinueButton.setEnabled, mContinueButton.setEnabled을 (참된);" onResume()에서 을 제거하고 "mContinueButton.setEnabled (true);"를 제거합니다. onClick()의 ​​줄? 아니면 다른 활동을 변경해야합니까? 왜냐하면 나는 당신이 말한 것을했기 때문에 버튼은 항상 활성화되어 있습니다. 또한, mContinueButton.setEnabled (false); 노란색 상자에 나타납니다 : http://i.imgur.com/wfbjWPV.jpg – Animated24

+0

답변에 대한 자세한 내용을 추가했습니다. – greeble31