In Android development, we often need to pop up some dialog boxes on the Android interface, such as asking the user or letting the user choose. These features we call it the Android Dialog dialog box, Alertdialog implementation method for Builder mode. Below we simulate the most common warning dialog box that pops up when uninstalling an application, as shown in the following figure:
Layout Layout Interface code example:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "android:layout_width=" match_parent "
android:layout_height=" Match_parent ">
<button
android:text= Uninstall"
android:layout_width= "Match_parent"
android: layout_height= "Wrap_content"
android:onclick= "show"
android:id= "@+id/button"/>
</ Linearlayout>
Java implementation code:
Import Android.content.DialogInterface;
Import Android.os.Bundle;
Import android.support.annotation.Nullable;
Import Android.support.v7.app.AlertDialog;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.View;
Import Android.widget.Toast;
/** * Created by Panchengjia on 2016/11/21. */public class Alertdialogdemo extends appcompatactivity {@Override protected void onCreate (@Nullable Bundle Savedin
Stancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.alterdialog);
} public void Show (View v) {//instantiated builder Alertdialog.builder Builder = new Alertdialog.builder (this);
Sets the caption Builder.settitle ("Uninstall") of the warning dialog box; Set the picture//Builder.seticon (Android) that the warning displays.
R.drawable.ic_dialog_alert);
Set the prompt message for the warning dialog box Builder.setmessage ("OK Uninstall"); Set the "Front" button, and click event Builder.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.maketExt (Alertdialogdemo.this, "Click the OK button", Toast.length_short). Show ();
}
}); Set the "Back" button, and click event Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.maketext (Alertdialogdemo.this, "Click the Cancel button", Toast.length_short
). Show ();
}
}); Set "neutral" button, and click event Builder.setneutralbutton ("Wait and see", New Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.maketext (Alertdialogdemo.this, "clicked Neutral button", Toast.length_shor
T). Show ();
}
});
Display dialog box Builder.show ();
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.