【问题标题】:Android Studio convert alertdialog edittext to intAndroid Studio 将 alertdialog edittext 转换为 int
【发布时间】:2017-08-02 22:45:34
【问题描述】:

我正在制作一个游戏,其中玩家在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();

【问题讨论】:

标签: android android-alertdialog


【解决方案1】:

您可以通过解析input 的文本来返回int

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

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

【讨论】:

  • 当我添加此行时尝试运行应用程序时出现错误。
  • @Pecktos 通过“描述”错误,我的意思是提供与其相关的 all 输出,例如Android Logcat 条目。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 2021-12-30
  • 2013-02-08
  • 2020-10-10
  • 2021-06-12
  • 2017-07-09
相关资源
最近更新 更多