【问题标题】:How to return value from new object instance method declaration (nested)?如何从新对象实例方法声明(嵌套)中返回值?
【发布时间】:2012-04-10 13:17:17
【问题描述】:

好的,这是代码:

公共布尔警报对话框(字符串消息){

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(message).setCancelable(false).setPositiveButton("Yes",        new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            TestBedAppActivity.this.agree = true;
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            TestBedAppActivity.this.agree = false;
            dialog.cancel();
        }

    });

    AlertDialog alert = builder.create();
    alert.show();

}

在 setPositiveButton() 方法中,有一个方法 onClick() 的嵌套声明。我想返回主方法 alertDialog(String message) 的布尔结果,但我做不到。我错过了什么?救命啊!!!!!!

【问题讨论】:

  • 你想做什么?描述“做不到”
  • 我想从 onClick() 方法内部返回主方法(外层)的布尔值。

标签: java android


【解决方案1】:

onClick 在用户单击肯定按钮或取消按钮时运行。当您调用“alert.show()”时,此方法立即返回。所以你不知道返回什么,因为用户操作没有发生。

【讨论】:

  • 您可以将代码分离为显示的对话框代码和来自用户操作的响应代码。
  • 很抱歉,我不知道任何解决方案。您的代码在 UI 线程上运行,您不得挂断代码等待用户操作。
猜你喜欢
  • 1970-01-01
  • 2012-08-17
  • 1970-01-01
  • 2014-01-11
  • 2016-09-02
  • 2013-07-20
  • 1970-01-01
  • 2020-05-22
  • 2013-02-02
相关资源
最近更新 更多