【问题标题】:android prompt user's input using a dialogandroid使用对话框提示用户输入
【发布时间】:2014-05-05 02:43:46
【问题描述】:

我想提示用户使用对话框在我的 android 应用程序中给我输入。这是我发现的:

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Title");
alert.setMessage("Message");

// Set an EditText view to get user input 
final EditText input = new EditText(this);
alert.setView(input);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText();
 // Do something with value!
 }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int whichButton) {
     // Canceled.
}
});

 alert.show();

但是这个 给我:

android.view.WindowManager$BadTokenException: 无法添加窗口 -- 令牌 null 不适用于应用程序

我的代码是否有任何问题,似乎在对话框中传递了一个空参数,但我无法找出问题所在。

【问题讨论】:

  • String value = input.getText().toString();

标签: android


【解决方案1】:

当我在一个新项目中运行您的代码时,它运行良好。所以可能是你正在使用的“这个”

  • 不是活动
  • 不是视图中的活动,即可能存在父活动。如果它是某个活动的子活动,请使用 getParent() 而不是“this”。
  • 为空

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我编写了一个帮助类,只需几行代码即可轻松创建提示对话框。

    PromptDialog dlg = new PromptDialog(MainActivity.this, R.string.title, R.string.enter_comment) {
     @Override
     public boolean onOkClicked(String input) {
      // do something
      return true; // true = close dialog
     }
    };
    dlg.show();
    

    See full code => Prompt Dialog for Android

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多