从自定义对话框返回数据

Jor*_*ren 5 android

我正在调用一个自定义对话框

        CustomDialog dialog = new CustomDialog(this);
        dialog.setCancelable(true);
        dialog.show();
Run Code Online (Sandbox Code Playgroud)

现在,如果对话框中有一堆按钮,当我解除()对话框时,如何返回用户的选择?

kru*_*hah 2

您可以参考此链接 http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

例子:

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");

TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
Run Code Online (Sandbox Code Playgroud)

您还可以使用警报对话框进行自定义

AlertDialog.Builder builder;
AlertDialog alertDialog;


Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)   Context.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
                           (ViewGroup) findViewById(R.id.layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
Run Code Online (Sandbox Code Playgroud)