如何在android中单击或不单击按钮上的if else条件?

时间:2017-08-04 17:12:10

标签: android if-statement

我想知道如何设置if else条件,如果没有单击任何按钮,则必须禁用下一个按钮。否则,他们能够继续下一个问题吗?

private AdvancedQuestion nAdvancedQuestion = new AdvancedQuestion();

private TextView nScoresView;
private TextView nQuestionsView;
private TextView tvTime;
private Button nButtonChoices1;
private Button nButtonChoices2;
private Button nButtonChoices3;
private Button nButtonChoices4;


private String nAnswers;
private int nScores = 0;
private int nQuestionNumbers = 0;

Button btnNextz;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_advanced_quiz);


    updateQuestions();

    nButtonChoices1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (nButtonChoices1.getText() == nAnswers) {
                correctSound.start();
                nScores = nScores + 1;
                nButtonChoices1.setEnabled(false);
                nButtonChoices2.setEnabled(false);
                nButtonChoices3.setEnabled(false);
                nButtonChoices4.setEnabled(false);
                nButtonChoices1.getBackground().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
                Toast.makeText(advancedQuiz.this, "correct", Toast.LENGTH_SHORT).show();

            } else {
                wrongSound.start();
                Toast.makeText(advancedQuiz.this, "wrong", Toast.LENGTH_SHORT).show();
                nButtonChoices1.setEnabled(false);
                nButtonChoices2.setEnabled(false);
                nButtonChoices3.setEnabled(false);
                nButtonChoices4.setEnabled(false);

            }
        }
    });

    btnNextz.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            nextSound.start();
            updateQuestions();
            nButtonChoices1.setEnabled(true);
            nButtonChoices2.setEnabled(true);
            nButtonChoices3.setEnabled(true);
            nButtonChoices4.setEnabled(true);
            nButtonChoices1.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
            nButtonChoices2.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
            nButtonChoices3.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
            nButtonChoices4.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);

}

private void updateQuestions() {
    nQuestionsView.setText(nAdvancedQuestion.getQuestions(nQuestionNumbers));
    nButtonChoices1.setText(nAdvancedQuestion.getChoices1(nQuestionNumbers));
    nButtonChoices2.setText(nAdvancedQuestion.getChoices2(nQuestionNumbers));
    nButtonChoices3.setText(nAdvancedQuestion.getChoices3(nQuestionNumbers));
    nButtonChoices4.setText(nAdvancedQuestion.getChoices4(nQuestionNumbers));

    nAnswers = nAdvancedQuestion.getCorrectAnswers(nQuestionNumbers);
    nQuestionNumbers++;
}

private void updateScore(int points) {
    nScoresView.setText("" + nScores);
}

请注意有4个可能的答案。如果没有选择它们,则在按下一个按钮之前它们无法继续下一个问题,因此他们可以转到下一个问题。 updateQuestions()是我相信它将显示下一个问题的部分。

2 个答案:

答案 0 :(得分:0)

这是一个关于如何根据if条件禁用/启用按钮的简单示例 -

// find and initialize any ~visible~ slick elements
$('.slick-carousel').each(function(){
    var slickHidden =$(this).parent().attr('aria-hidden');
    var target = $(this).attr('id');
    var view = $(this).attr('data-view');
    if(!slickHidden){
        slickInit(target,view);
    }
})


// init slick on tab/accordion click
$('[data-responsive-accordion-tabs]').on('change.zf.tabs down.zf.accordion', function(event, obj) {
    // target can be either a tab or link in accordion title
    var target = $(obj).attr('data-tab-target');
    if(!target){
        var target = $(obj).parent().attr('data-tab-target');
    }
    // test if target is already init, if no ~ init! 
    if(!$('#'+target).hasClass('slick-initialized')){
        slickInit(target);
    }

});


// initialize slick target
function slickInit(target,view){
    // initialize slick
}

另请查看此link

答案 1 :(得分:0)

您需要在要显示的活动视图中添加一个按钮元素,然后在活动代码中向其添加一个事件侦听器,或者在活动XML布局文件中指定要调用的函数。

请参阅:https://developer.android.com/reference/android/widget/Button.html