Android Studio将alertdialog edittext转换为int

时间:2017-03-13 00:14:13

标签: android alertdialog

我制作的游戏中玩家在AlertDialog框中输入了一个数字,我需要将此输入用作int变量。我怎么能得到它?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(this); //the number
input.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(input);
builder.setTitle("Begin Game");
builder.setMessage("How Many rounds do you want?");
builder.setCancelable(true);
builder.setPositiveButton("OK", new HandleAlertDialogListener());
AlertDialog dialog = builder.create();
dialog.show();

1 个答案:

答案 0 :(得分:0)

您可以通过解析int的文字来获得input

 final int rounds = Integer.parseInt(input.getText().toString());

注意Integer.parseInt在无法解析数字时会抛出NumberFormatException