java - 通过多个函数返回

标签 java android

好吧,这可能有点简单,但我不知道该怎么做。如何更改此函数以返回 String class_name?我知道我需要将函数从 void 更改为 String,但我还需要做什么?

非常感谢!

public void addClass() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);  

    alert.setTitle("Add Class");

    final EditText input = new EditText(this);  
    alert.setView(input);  

    alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  
            String class_name = input.getText().toString();
        }  
    });  

    alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  
        }  
    });  

    alert.show();
}

最佳答案

返回 class_name 值没有帮助。您必须实现一个回调方法来获取值并执行所需的操作:

public void addClass() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);  

    // ...

    alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  
            setClassName(input.getText().toString());
        }  
    });  

    // ...
}

protected void setClassName(String class_name) {
  // do what ever has to be done with class_name
}

关于java - 通过多个函数返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3624091/

相关文章:

java - 如何使用 Jackson Databind 反序列化嵌套泛型类?

java - 如何设计一个互斥但独立并发方法的任务队列?

javascript - 使用 PhoneGap 从设备的图片库中选择多张照片

android - 新的 Debug Keystore 是否意味着新的 Map API Key?

java - Bazel - 获取当前操作系统和 CPU 类型作为构建规则中使用的变量

java - Android SDK 管理器 MacOS 无法选择要安装的项目

java - 使用slick 2D导出eclipse项目时的一些问题

android - 我的应用程序在进入循环后卡住

android - 可以过滤 WifiManager 的扫描吗?

android - 错误: "Method getText() must be called from the UI thread, currently inferred thread is worker."