从对话框返回数据

时间:2010-07-12 17:28:33

标签: android dialog

我使用对话框作为用户输入数据的表单。当他们单击“确定”按钮时,对话框关闭,我需要使用输入的数据。对话框关闭后,如何在活动中引用此数据?

2 个答案:

答案 0 :(得分:5)

当用户按下“确定”按钮

时获取它
final EditText input = new EditText(this); // This could also come from an xml resource, in which case you would use findViewById() to access the input

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        String value = input.getText().toString();
        mItem.setValue(value); // mItem is a member variable in your Activity
        dialog.dismiss();
    }
});

答案 1 :(得分:4)

为对话框分配OnDismissListener并将数据传递给那里的活动。

或者,您可以创建对话框活动并将数据作为活动结果返回。有关开始活动和获得结果的更多信息,请参阅以下链接:

http://developer.android.com/reference/android/app/Activity.html#StartingActivities