如何在按钮中使用modulo并设置点击限制?

时间:2016-12-08 03:55:44

标签: java android button modulo

如何为标有帮助的按钮设置限制点击?我使用模运算符(即%)来划分 - 对于每5个正确答案,帮助按钮将添加加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;
}

1 个答案:

答案 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点击监听器中注释启用和禁用帮助按钮并尝试。

相关问题