从自定义对话框返回数据

时间:2010-10-22 00:56:16

标签: android

我正在调用自定义对话框

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

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

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

首先,通过方法findViewById()获取对话框中的所有按钮,然后添加一个 View.OnClickListener到按钮,在

View.OnClickListener::onClick()
{
  //Do something
  dismiss();
  //Do something.
}

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