从自定义对话框返回数据

5
我正在这样调用自定义对话框:

        CustomDialog dialog = new CustomDialog(this);
        dialog.setCancelable(true);
        dialog.show();

现在,如果对话框中有一堆按钮,当我使用dismiss()关闭对话框时,我如何返回用户的选择?
2个回答

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);

您也可以使用警告对话框进行自定义

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();

0
首先,通过方法findViewById()获取对话框中的所有按钮,然后将View.OnClickListener添加到按钮中,在其中。
View.OnClickListener::onClick()
{
  //Do something
  dismiss();
  //Do something.
}

在关闭对话框之前或之后,您可以执行一些操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接