我有一个警告对话框,可在状态结束时显示。此对话框显示分数和高分。当语句为真时,有没有办法显示特定的警告对话框?
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会包含第一组代码,只显示分数和当前的高分。
答案 0 :(得分:0)
我认为这应该适合你:
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输出添加到问题中以便还可以识别任何其他错误(如果存在),那将会很棒。