2016-12-08 16 views
1

と表示されたボタンのクリック制限を設定するにはどうすればよいですか?モジュロ演算子(%)を使用してスコアを分割します。ヘルプボタンにプラス5を加え、プラス1に+1して.setEnabled(true)を呼び出します。ヘルプボタンの値が0の場合、ボタンは無効になります。ここにヘルプボタンの状態を変更するためのコードがあります。ヘルプボタンを使用すると、2つの誤った答えを取り除くことができます。ボタンでモジュロを使用してクリック制限を設定する方法は?

public class QuestionActivity extends Activity { 

MediaPlayer mySound; 
List<Question> quesList; 
int score = 0; 
int qid = 0; 
int i = 0; 

//for help 50/50 
int help = score % 5; 
int rnd2 ,rnd1; 

ProgressBar mProgressBar; 
CountDownTimer mCountDownTimer; 

// Animation 
Animation animFadein; 


Question currentQ; 
TextView txtQuestion, scored; 
Button button1, button2, button3, button4,helpbtn; 
/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
private GoogleApiClient client; 
public void help (View view){ 

    helpbtn=(Button)findViewById(R.id.helpbtn); 
    helpbtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
    help = help - 1; 
      v.setEnabled(false); 

      /*if (help == 0){ 
       helpbtn.setEnabled(false); 
      } else { 
       helpbtn.setEnabled(true); 
      } 
      */ 
      String AnswerString = currentQ.getANSWER(); 
      //match DB answer to selected answer, turn it visible if it is correct 

      if(button1.getText().equals(AnswerString)){ 
       button1.setVisibility(View.VISIBLE); 
      } 
      if(button2.getText().equals(AnswerString)){ 
       button2.setVisibility(View.VISIBLE); 
      } 
      if(button3.getText().equals(AnswerString)){ 
       button3.setVisibility(View.VISIBLE); 
      } 
      if(button4.getText().equals(AnswerString)){ 
       button4.setVisibility(View.VISIBLE); 
      } 

      //random disable 2 incorrect answer 
      List<Integer> list = Arrays.asList(1, 2, 3, 4); 
      Collections.shuffle(list); 
      rnd1 = list.get(0); 
      rnd2 = list.get(1); 

      if ((rnd1 == 1) || (rnd2 == 1)){ 
       button1.getText().equals(AnswerString); 
       button1.setVisibility(View.INVISIBLE); 
      } 

      if ((rnd1 == 2) || (rnd2 == 2)){ 
       button2.getText().equals(AnswerString); 
       button2.setVisibility(View.INVISIBLE); 
      } 

      if ((rnd1 == 3) || (rnd2 == 3)){ 
       button3.getText().equals(AnswerString); 
       button3.setVisibility(View.INVISIBLE); 
      } 
      if ((rnd1 == 4) || (rnd2 == 4)){ 
       button4.getText().equals(AnswerString); 
       button4.setVisibility(View.INVISIBLE); 
      } 
     } 
    }); 
} 

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_qestion); 
    QuizHelper db = new QuizHelper(this); // my question bank class 
    quesList = db.getAllQuestions(); // this will fetch all questions 
    currentQ = quesList.get(qid); // the current question 
    mySound = MediaPlayer.create(this, R.raw.bensoundcute); // music background 
    mySound.start(); 
    mySound.setLooping(true); 

    txtQuestion = (TextView) findViewById(R.id.txtQuestion); 
    // load the textQuestion animation 
    animFadein = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in); 
    // the text view in which the question will be displayed 
    // the 4 buttons, 
    // the idea is to set the text of 4 buttons with the options from question bank 
    button1 = (Button) findViewById(R.id.button1); 
    button2 = (Button) findViewById(R.id.button2); 
    button3 = (Button) findViewById(R.id.button3); 
    button4 = (Button) findViewById(R.id.button4); 

    // the text view in which score will be displayed 
    scored = (TextView) findViewById(R.id.score); 
    // method which will set the things up for our game 
    setQuestionView(false); 

    mProgressBar = (ProgressBar) findViewById(progressbar); 
    mProgressBar.setMax(100); 
    mProgressBar.setProgress(i); 




    mCountDownTimer = new CountDownTimer(30000,300) { 
      @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) 
      @Override 
      public void onTick(long millisUntilFinished) { 
       Log.v("Log_tag", "Timer Progress " + i + millisUntilFinished); 
       i++; 
       mProgressBar.setRotation(180); 
       mProgressBar.setProgress(i); 

      } 
      @Override 
      public void onFinish() { 
       Toast.makeText(getApplicationContext(), "TIME is UP!", Toast.LENGTH_SHORT).show(); 
       Intent intent = new Intent(QuestionActivity.this, 
         ResultActivity.class); 
       // passing the int value 
       Bundle b = new Bundle(); 
       b.putInt("score", score); // Your score 
       intent.putExtras(b); // Put your score to your next 
       startActivity(intent); 
       mCountDownTimer.cancel(); 
       finish(); 
      } 
    }; 
    txtQuestion.setAnimation(animFadein); 
    txtQuestion.startAnimation(animFadein); 
    mCountDownTimer.start(); 
    // button click listeners 
    final MediaPlayer buttonSound = MediaPlayer.create(this, R.raw.play); 
    button1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      buttonSound.start(); 
      if(mCountDownTimer!=null){ 
       mCountDownTimer.cancel(); 
      } 

      // passing the button text to other method 
      // to check whether the answer is correct or not 
      // same for all three buttons 
      getAnswer(button1.getText().toString()); 

     } 
    }); 
    button2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      buttonSound.start(); 
      if(mCountDownTimer!=null){ 
       mCountDownTimer.cancel(); 
      } 

      getAnswer(button2.getText().toString()); 

     } 
    }); 
    button3.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      buttonSound.start(); 
      if(mCountDownTimer!=null){ 
       mCountDownTimer.cancel(); 
      } 
      getAnswer(button3.getText().toString()); 


     } 
    }); 

    button4.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      buttonSound.start(); 
      if(mCountDownTimer!=null){ 
       mCountDownTimer.cancel(); 
      } 

      getAnswer(button4.getText().toString()); 
     } 
    }); 




    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
} 
@Override 
protected void onPause(){ 
    super.onPause(); 
    mySound.release(); 
    finish(); 
} 
@Override 
public void onBackPressed() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setCancelable(false); 
    builder.setMessage("Do you want to Exit?"); 
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      //if user pressed "yes", then he is allowed to exit from application 
      dialog.dismiss(); 
      onYesClick(); 
     } 
     private void onYesClick() { 
      Intent setIntent = new Intent(Intent.ACTION_MAIN); 
      setIntent.addCategory(Intent.CATEGORY_HOME); 
      setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(setIntent); 
      mCountDownTimer.cancel(); 
      finish(); 
      QuestionActivity.this.finish(); 
     } 
    }); 
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      //if user select "No", just cancel this dialog and continue with app 
      dialog.cancel(); 
     } 
    }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
} 
public void getAnswer(String AnswerString) { 

    if (currentQ.getANSWER().equals(AnswerString)) { 
     // if conditions matches increase the int (score) by 1 
     // and set the text of the score view 
     // Intent intent = new Intent(this, QuestionActivity.class); 
     //startActivity(intent); 
     Toast.makeText(getApplicationContext(), "CORRECT!", Toast.LENGTH_SHORT).show(); 
     score++; 
     scored.setText("Score: " + score + " /100"); 
     txtQuestion.setAnimation(animFadein); 
     txtQuestion.startAnimation(animFadein); 

    } 
    else { 
     setQuestionView(false); 
     // if unlucky start activity and finish the game 
     Toast.makeText(getApplicationContext(), "Sorry! Better luck next time.", Toast.LENGTH_SHORT).show(); 
     Intent intent = new Intent(QuestionActivity.this, ResultActivity.class); 
     // passing the int value 
     Bundle b = new Bundle(); 
     b.putInt("score", score); // Your score 
     intent.putExtras(b); // Put your score to your next 
     startActivity(intent); 
     mCountDownTimer.cancel(); 
     finish(); 
    } 


    if(qid < 100) { 
     // if questions are not over then do this 
     currentQ = quesList.get(qid); 
     setQuestionView(true); 
     txtQuestion.setAnimation(animFadein); 
     txtQuestion.startAnimation(animFadein); 

     button1.setVisibility(View.VISIBLE); 
     button2.setVisibility(View.VISIBLE); 
     button3.setVisibility(View.VISIBLE); 
     button4.setVisibility(View.VISIBLE); 

    } 
    else { 
     Toast.makeText(getApplicationContext(), 
       "Game Over.", 
       Toast.LENGTH_SHORT) 
       .show(); 
     Intent intent = new Intent(QuestionActivity.this, 
       ResultActivity.class); 
     // passing the int value 
     Bundle b = new Bundle(); 
     b.putInt("score", score); // Your score 
     intent.putExtras(b); // Put your score to your next 
     startActivity(intent); 
     mCountDownTimer.cancel(); 
     finish(); 
    } 
} 

private boolean setQuestionView(boolean b) { 

    // the method which will put all things together 
    txtQuestion.setText(currentQ.getQUESTION()); 
    button1.setText(currentQ.getOPTA()); 
    button2.setText(currentQ.getOPTB()); 
    button3.setText(currentQ.getOPTC()); 
    button4.setText(currentQ.getOPTD()); 

    qid++; 
    return b; 
} 
+0

コードに問題がありますか? – Vinodh

+0

私は5つの正しい答えを得るたびにヘルプボタンはクリックできません。 –

+0

正しい答えが格納されている場所を表示 –

答えて

0
if (currentQ.getANSWER().equals(AnswerString)) { 
    // if conditions matches increase the int (score) by 1 
    // and set the text of the score view 
    // Intent intent = new Intent(this, QuestionActivity.class); 
    //startActivity(intent); 
    Toast.makeText(getApplicationContext(), "CORRECT!", Toast.LENGTH_SHORT).show(); 
    score++; 
    scored.setText("Score: " + score + " /100"); 
    txtQuestion.setAnimation(animFadein); 
    txtQuestion.startAnimation(animFadein); 


     if (score%5 == 1){ 
      helpbtn.setEnabled(false); 
     } else { 
      helpbtn.setEnabled(true); 
     } 


} 

ノート:hlpbuttonクリックリスナーでヘルプボタンの有効化および無効化コメントとしてみてください。

+0

ありがとうございました。値のヘルプボタンが0の場合は、次の質問でヘルプを無効にする必要があります。私はヘルプボタンのクリックを5つの正解ごとに制限する必要があります。ヘルプボタンは、クリック後にプラス1とマイナス1になります。 –

+0

このロジックは、ヘルプボタン自体に書くことができます。有効化と無効化については、getAnswer()のスコアを確認する必要があります –

+0

スコアを確認して5に分けるにはどうすればよいですか?私はヘルプボタンでクリックの限界を設定したい。私はモジュロint help = score%5を使用しました。 –

関連する問題