弹出窗口排在第一或第二

时间:2018-11-28 03:30:52

标签: java android-studio tic-tac-toe

我正在写一个简单的井字游戏,每次赢或输之后,我都希望玩家选择他们要先走还是第二(X或0)。获取此信息的最佳策略是什么?

1 个答案:

答案 0 :(得分:0)

您可以为此显示AlertDialog,让RadioButton的0和X成为给玩家提供选项的简单推荐方法。

请参阅示例代码以供参考:

AlertDialog playerDialog;

// Strings to Show In Dialog with Radio Buttons
final CharSequence[] items = {" Zero "," Cross "};

 // Creating and Building the Dialog 
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Your Player");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item)
{
case 0:
// Your code when first option selected
break;
case 1:
// Your code when 2nd  option selected
break;
}
playerDialog.dismiss();    
}
});
playerDialog = builder.create();
playerDialog.show();