开发者

Return data from Custom Dialog

开发者 https://www.devze.com 2023-01-21 01:56 出处:网络
I\'m calling a custom dialog thusly CustomDialog dialog = new CustomDialog(this); dialog.setCancelable(true);

I'm calling a custom dialog thusly

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

Now, if I have a bunch of buttons in t开发者_Python百科he dialog, how do I return the user's choice when I dismiss() the dialog?


You can refer this link http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

Example:

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

You can also use alert dialog for custom

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


First, get all buttons in your dialog through method findViewById() then add a View.OnClickListener to the button,in the

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

You can do something before or after dismiss the dialog.

0

精彩评论

暂无评论...
验证码 换一张
取 消