A common approach:
[HTML] view Plaincopyprint?
-
Alertdialog.builder Builder;
-
Alertdialog Alertdialog;
-
-
Layoutinflater inflater = Getlayoutinflater ();
-
Add a custom layout file
-
View layout = Layoutinflater.from (testone.this). Inflate (
-
R.layout.dialog, NULL);
-
Final TextView Text = (TextView) Layout.findviewbyid (R.ID.TV1);
-
Add a Click event
-
Text.setonclicklistener (New Onclicklistener () {
-
-
@Override
-
public void OnClick (View v) {
-
TODO auto-generated Method Stub
-
Text.settext ("call");
-
}
-
});
-
-
Builder = new Alertdialog.builder (testone.this);
-
Alertdialog = Builder.create ();
-
Remove the black border, because the spacing is set to 0 with four weeks
-
Alertdialog.setview (layout, 0, 0, 0, 0);
-
Alertdialog.show ();
-
Modify Size
-
Windowmanager.layoutparams params = Alertdialog.getwindow ()
-
. GetAttributes ();
-
Params.width = 350;
-
Params.height = 200;
-
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?
-
/**
-
* Customized dialog box
-
*/
-
Public abstract class Myalerdialog extends Alertdialog implements
-
Android.view.View.OnClickListener {
-
-
Protected Myalerdialog (Context context) {
-
Super (context);
-
TODO auto-generated Constructor stub
-
}
-
-
/**
-
* One of the components in the layout
-
*/
-
Private TextView txt;
-
-
@Override
-
protected void OnCreate (Bundle savedinstancestate) {
-
TODO auto-generated Method Stub
-
Super.oncreate (savedinstancestate);
-
Loading a custom layout
-
Setcontentview (R.layout.dialog);
-
Setdialogsize (300, 200);
-
txt = (TextView) Findviewbyid (R.ID.TV1);
-
Txt.setonclicklistener (this);
-
}
-
-
/**
-
* Modify Frame body size
-
*
-
* @param width
-
* @param height
-
*/
-
public void setdialogsize (int width, int height) {
-
Windowmanager.layoutparams params = GetWindow (). GetAttributes ();
-
Params.width = 350;
-
Params.height = 200;
-
This.getwindow (). SetAttributes (params);
-
}
-
-
public abstract void Clickcallback ();
-
-
/**
-
* Click events
-
*/
-
@Override
-
public void OnClick (View v) {
-
TODO auto-generated Method Stub
-
if (v = = txt) {
-
Clickcallback ();
-
}
-
}
-
-
}
To use in an activity:
[HTML] view Plaincopyprint?
-
Myalerdialog Mydialog = new Myalerdialog (this) {
-
Overriding the callback method
-
@Override
-
public void Clickcallback () {
-
TODO auto-generated Method Stub
-
Btn.settext ("call");
-
}
-
};
-
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