点击按钮时更改背景颜色。

huangapple go评论54阅读模式
英文:

Change bgcolor when click button

问题

当答案按钮被点击时,应该将其背景颜色更改为不同的颜色。我是这样实现的:

selectionBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 正确答案被点击
if (checkChosen(questionProgress, selectionBtn.getText().toString())) {
// 将背景颜色更改为绿色,表示用户选择了正确答案
selectionBtn.setBackgroundResource(R.drawable.correct_button_selected);
// 将背景颜色保持0.3秒
selectionBtn.postDelayed(new Runnable() {
@Override
public void run() {
selectionBtn.setBackgroundResource(R.drawable.choice_button_border);
}
}, 300);
} else {
// 错误答案被点击
selectionBtn.setBackgroundResource(R.drawable.wrong_button_selected);
selectionBtn.postDelayed(new Runnable() {
@Override
public void run() {
selectionBtn.setBackgroundResource(R.drawable.choice_button_border);
}
}, 300);
}
// 进度增加
questionProgress++;
/**
* 进度等于问题数量,表示最后一个问题
* 显示提交按钮
/
if (questionProgress == questionAmount) {
binding.submitBtn.setVisibility(View.VISIBLE);
binding.progressBar.setProgress(binding.progressBar.getMax());
binding.progressCount.setText(questionAmount + " / " + questionAmount);
}
/
*
* 进度 < 数量,移除上一个问题的答案按钮
* 添加新的答案按钮,并更改进度条
*/
if (questionProgress < questionList.length) {
Log.v("progress", String.valueOf(questionProgress));
binding.progressBar.setProgress(questionProgress);
binding.progressCount.setText(questionProgress + " / " + questionAmount);
binding.quizContent.setText(questionList[questionProgress]);
binding.selectionLayout.removeAllViews();
String[] anLi = answerMap.get(questionProgress);
for (int i = 0; i < anLi.length - 1; i++) {
addSelections(anLi[i]);
}
}
}
});

然而,用这种方式只有最后一个问题的答案按钮在被点击时才会改变背景颜色。

如何解决这个问题?提前感谢。
英文:

I'm using Android to create a Quiz application. So far, most of the things are done. Only one problem. When the answer button is clicked, its background color should be changed to a different color.

I implement in this way:

selectionBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                correct answer clicked
                if (checkChosen(questionProgress, selectionBtn.getText().toString())) {
//                    change the bg-color to green indicates user made correct choice
                    selectionBtn.setBackgroundResource(R.drawable.correct_button_selected);
//                    last the bg-color for 0.3s
                    selectionBtn.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            selectionBtn.setBackgroundResource(R.drawable.choice_button_border);
                        }
                    }, 300);
                } else {
//                    wrong answer clicked
                    selectionBtn.setBackgroundResource(R.drawable.wrong_button_selected);
                    selectionBtn.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            selectionBtn.setBackgroundResource(R.drawable.choice_button_border);
                        }
                    }, 300);
                }
//                progress added
                questionProgress++;
                /**
                 * progress equals the amount, means the last question
                 * show the submit button
                 */
                if (questionProgress == questionAmount) {
                    binding.submitBtn.setVisibility(View.VISIBLE);
                    binding.progressBar.setProgress(binding.progressBar.getMax());
                    binding.progressCount.setText(questionAmount + &quot; / &quot; + questionAmount);
                }
                /**
                 * progress &lt; amount, remove answer buttons of the last question
                 * add new answer buttons and change progress bar
                 */
                if (questionProgress &lt; questionList.length) {
                    Log.v(&quot;progress&quot;, String.valueOf(questionProgress));
                    binding.progressBar.setProgress(questionProgress);
                    binding.progressCount.setText(questionProgress + &quot; / &quot; + questionAmount);
                    binding.quizContent.setText(questionList[questionProgress]);
                    binding.selectionLayout.removeAllViews();
                    String[] anLi = answerMap.get(questionProgress);
                    for (int i = 0; i &lt; anLi.length - 1; i++) {
                        addSelections(anLi[i]);
                    }
                }
            }
        });

However, in this way, only answer buttons for the last question will change bg-color when they are clicked.

How can I solve this problem? Thanks in advance.

答案1

得分: 0

你可以在onClick方法中像下面的代码片段中一样使用view

selectionBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View view) {
        if (checkChosen(questionProgress, selectionBtn.getText().toString())) {
            view.setBackgroundResource(R.drawable.correct_button_selected);

            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    view.setBackgroundResource(R.drawable.choice_button_border);
                }
            }, 200);
        } else {
            // else condition
        }
    }
});
英文:

You can use view from onClick method like in the code snippet below.

selectionBtn.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(final View view) {
		if (checkChosen(questionProgress, selectionBtn.getText().toString())) {
			view.setBackgroundResource(R.drawable.correct_button_selected);

			final Handler handler = new Handler();
			handler.postDelayed(new Runnable() {
				@Override
				public void run() {
					view.setBackgroundResource(R.drawable.choice_button_border);
				}
			}, 200);
		} else {
			// else condition
		}
	}
});

答案2

得分: 0

你似乎在为每个答案创建新按钮时重复使用了 selectionBtn 变量。当延迟运行开始时,selectionBtn 的值将不同,它将指向您创建的最后一个按钮。

要修复这个问题,可以使用 onClick() 方法的参数,实际上它是触发事件的视图(也就是您正确的按钮):

selectionBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        final Button currentButton = (Button) view;
        
        if (checkChosen(questionProgress, currentButton.getText().toString())) {
            currentButton.setBackgroundResource(R.drawable.correct_button_selected);
        } else {
            currentButton.setBackgroundResource(R.drawable.wrong_button_selected);
        }

        currentButton.postDelayed(new Runnable() {
            @Override
            public void run() {
                currentButton.setBackgroundResource(R.drawable.choice_button_border);
            }
        }, 300);

        ....
    }
英文:

You seem to reuse selectionBtn variable while creating new button for each answer, when your delayed runnable starts, the value of selectionBtn will be different and it will be pointing the last button you created.

To fix that, use the onClick()'s argument, which actually is the view that triggered the event (which is your correct button):

selectionBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        final Button currentButton = (Button) view;
        
        if (checkChosen(questionProgress, currentButton.getText().toString())) {
            currentButton.setBackgroundResource(R.drawable.correct_button_selected);
        } else {
            currentButton.setBackgroundResource(R.drawable.wrong_button_selected);
        }

        currentButton.postDelayed(new Runnable() {
            @Override
            public void run() {
                currentButton.setBackgroundResource(R.drawable.choice_button_border);
            }
        }, 300);

        ....
    }

答案3

得分: 0

我已修复了它。问题出在 removeAllViews() 上。在添加新按钮之前,它会移除所有答案按钮的视图,这会导致闪烁不可见,因为它被移除。

这是我修复它的方式。

英文:

I have fixed it. The problem is removeAllViews(). It removes the whole views for answer buttons before add new buttons, and that makes the flash not visible cause it's removed.

if (questionProgress &lt; questionList.length) {
                    Log.v(&quot;progress&quot;, String.valueOf(questionProgress));
                    binding.progressBar.setProgress(questionProgress);
                    binding.progressCount.setText(questionProgress + &quot; / &quot; + questionAmount);
                    binding.quizContent.setText(questionList[questionProgress]);
                    binding.selectionLayout.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            binding.selectionLayout.removeAllViews();
                            String[] anLi = answerMap.get(questionProgress);
                            for (int i = 0; i &lt; anLi.length - 1; i++) {
                                addSelections(anLi[i]);
                            }
                        }
                    }, 300);

                }

This is how I fixed it.

huangapple
  • 本文由 发表于 2023年4月17日 18:37:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定