当一个状态结束时,我有一个警报对话框要显示。此对话框显示分数和高分。当语句为true时,有没有办法显示特定的警报对话框?
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Score: " + Score + " High Score: " + highScore)
.setPositiveButton(R.string.returnString, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getActivity(), GameState.class);
startActivity(intent);
}
});
return builder.create();
我在下面尝试了一下,但没有成功,只是导致应用程序崩溃。
if(Score > highScore) {
builder.setMessage("You have set a new high score: " + highScore)
.setPositiveButton(R.string.returnString, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getActivity(),GameState.class);
startActivity(intent);
}
});
然后,else将包含第一组代码,仅显示分数和当前高分。
我认为这应该适用于您:
String message = "Score: " + Score + " High Score: " + highScore;
if(Score > highScore) {
message = "You have set a new high score: " + highScore;
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(message)
.setPositiveButton(R.string.returnString, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getActivity(), GameState.class);
startActivity(intent);
}
});
return builder.create();
这将解决条件消息显示问题。然而,如果您将logcat输出添加到问题中,以便也可以识别任何其他错误(如果存在),那就太好了。