Java 显示基于if/else语句的对话框
我有一个非常简单的方法,如果点击次数等于4、8、12,则输出相关消息,否则每4个数字输出一条一般消息。这些都显示在对话框中,但我注意到,单击次数每增加一次,就会出现else语句。我不知道这是为什么 我也不明白的是,每次对话框出现时,都需要更多的尝试来关闭它。例如,当它第一次打开时,我点击ok按钮一次,它就关闭了。当它第二次打开时,需要单击2次“确定”以关闭它。·倍等于3倍,以此类推Java 显示基于if/else语句的对话框,java,android,Java,Android,我有一个非常简单的方法,如果点击次数等于4、8、12,则输出相关消息,否则每4个数字输出一条一般消息。这些都显示在对话框中,但我注意到,单击次数每增加一次,就会出现else语句。我不知道这是为什么 我也不明白的是,每次对话框出现时,都需要更多的尝试来关闭它。例如,当它第一次打开时,我点击ok按钮一次,它就关闭了。当它第二次打开时,需要单击2次“确定”以关闭它。·倍等于3倍,以此类推 selectAnotherButton.setOnClickListener(new View.O
selectAnotherButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getClickCountInt++;
if (getClickCountInt == 4){
ShowRewardDialog("You are rewarded with a the yellow smiley face in the homepage");
} else if (getClickCountInt == 8) {
ShowRewardDialog("You are rewarded with a the green smiley face in the homepage");
} else if (getClickCountInt == 12) {
ShowRewardDialog("You are rewarded with a the red smiley face in the homepage");
} else {
for(int i = 0; i <= getClickCountInt; i+=4) {
ShowRewardDialog("You are rewarded with a video\"");
}
}
}
});
private void ShowRewardDialog(String message) {
final Dialog dialog = new Dialog(Content.this);
dialog.setContentView(R.layout.custom_dialog);
SpannableString title = new SpannableString("YOU GAINED A REWARD");
title.setSpan(new ForegroundColorSpan(Content.this.getResources().getColor(R.color.purple))
, 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// set the custom dialog components - text, image and button
TextView text = dialog.findViewById(R.id.dialog_text);
dialog.setTitle(title);
text.setText(message);
Button dialogButton = dialog.findViewById(R.id.dialog_button_OK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
发生这种情况的原因是您正在同一位置打开多个窗口,这是由以下循环引起的:
for(int i = 0; i <= getClickCountInt; i+=4) {
ShowRewardDialog("You are rewarded with a video\"");
}
进行更多尝试的原因是,例如9,它在同一位置显示了一个新对话框三次,该对话框在0、4和8上作为for循环函数,因此使用break;在循环中,只显示一个对话框,或使用布尔逻辑或其他方法,在显示新对话框之前检查已可见的对话框。已修复该对话框。非常感谢。
if(getClickCountInt % 4 == 0){
ShowRewardDialog("You are rewarded with a video\"");
}