Android Custom Alertdialog

Source: Internet
Author: User

A common approach:

[HTML] view Plaincopyprint?

  1. Alertdialog.builder Builder;

  2. Alertdialog Alertdialog;

  3. Layoutinflater inflater = Getlayoutinflater ();

  4. Add a custom layout file

  5. View layout = Layoutinflater.from (testone.this). Inflate (

  6. R.layout.dialog, NULL);

  7. Final TextView Text = (TextView) Layout.findviewbyid (R.ID.TV1);

  8. Add a Click event

  9. Text.setonclicklistener (New Onclicklistener () {

  10. @Override

  11. public void OnClick (View v) {

  12. TODO auto-generated Method Stub

  13. Text.settext ("call");

  14. }

  15. });

  16. Builder = new Alertdialog.builder (testone.this);

  17. Alertdialog = Builder.create ();

  18. Remove the black border, because the spacing is set to 0 with four weeks

  19. Alertdialog.setview (layout, 0, 0, 0, 0);

  20. Alertdialog.show ();

  21. Modify Size

  22. Windowmanager.layoutparams params = Alertdialog.getwindow ()

  23. . GetAttributes ();

  24. Params.width = 350;

  25. Params.height = 200;

  26. Alertdialog.getwindow (). SetAttributes (params);


This way, the custom layout view is refilled, but it lacks extensibility and is redefined every time it is used.

To override the Alertdialog class, define the method:

[HTML] view Plaincopyprint?

  1. /**

  2. * Customized dialog box

  3. */

  4. Public abstract class Myalerdialog extends Alertdialog implements

  5. Android.view.View.OnClickListener {

  6. Protected Myalerdialog (Context context) {

  7. Super (context);

  8. TODO auto-generated Constructor stub

  9. }

  10. /**

  11. * One of the components in the layout

  12. */

  13. Private TextView txt;

  14. @Override

  15. protected void OnCreate (Bundle savedinstancestate) {

  16. TODO auto-generated Method Stub

  17. Super.oncreate (savedinstancestate);

  18. Loading a custom layout

  19. Setcontentview (R.layout.dialog);

  20. Setdialogsize (300, 200);

  21. txt = (TextView) Findviewbyid (R.ID.TV1);

  22. Txt.setonclicklistener (this);

  23. }

  24. /**

  25. * Modify Frame body size

  26. *

  27. * @param width

  28. * @param height

  29. */

  30. public void setdialogsize (int width, int height) {

  31. Windowmanager.layoutparams params = GetWindow (). GetAttributes ();

  32. Params.width = 350;

  33. Params.height = 200;

  34. This.getwindow (). SetAttributes (params);

  35. }

  36. public abstract void Clickcallback ();

  37. /**

  38. * Click events

  39. */

  40. @Override

  41. public void OnClick (View v) {

  42. TODO auto-generated Method Stub

  43. if (v = = txt) {

  44. Clickcallback ();

  45. }

  46. }

  47. }

To use in an activity:

[HTML] view Plaincopyprint?

  1. Myalerdialog Mydialog = new Myalerdialog (this) {

  2. Overriding the callback method

  3. @Override

  4. public void Clickcallback () {

  5. TODO auto-generated Method Stub

  6. Btn.settext ("call");

  7. }

  8. };

  9. Mydialog.show ();

Self-written features are encapsulated in two, the need for children's shoes can be easily extended. This method is obviously more advantageous than the previous one.

Android Custom Alertdialog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.