Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.app.Dialog;
Import Android.app.ProgressDialog;
Import Android.content.DialogInterface;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.Toast;
Public class Mainactivity extends Activity implements Onclicklistener {
//control type
Private Button button1;
Private Alertdialog.builder Builder;
Private Button button2;
Private Button Button3;
Private Button Button4;
Private Button button5;
Private Button Button6;
Private Button button7;
Private Dialog Dialog;
Private View Button8;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Find controls
Button1 = (Button) Findviewbyid (R.id.button1);
Button2 = (Button) Findviewbyid (R.id.button2);
Button3 = (Button) Findviewbyid (R.id.button3);
Button4 = (Button) Findviewbyid (R.ID.BUTTON4);
Button5 = (Button) Findviewbyid (R.ID.BUTTON5);
Button6 = (Button) Findviewbyid (R.ID.BUTTON6);
button7 = (Button) Findviewbyid (r.id.button7);
Button8 = (Button) Findviewbyid (R.ID.BUTTON8);
To add a listener to a control
Button1.setonclicklistener (this);
Button2.setonclicklistener (this);
Button3.setonclicklistener (this);
Button4.setonclicklistener (this);
Button5.setonclicklistener (this);
Button6.setonclicklistener (this);
Button7.setonclicklistener (this);
Button8.setonclicklistener (this);
Dialog ();
}
@Override
public void OnClick (View v) {
Switch to determine the user's operation, implement the function
Switch (V.getid ()) {
Case R.id.button1:
Builder.create (). Show ();
Break
Case R.id.button2:
Dialogg ();
Dialog.show ();
Break
Case R.ID.BUTTON3:
New Alertdialog.builder (this). Settitle ("Please enter"). SetIcon (
Android. R.drawable.ic_dialog_info). Setview (
New EditText (This)). Setpositivebutton ("OK", NULL)
. Setnegativebutton ("Cancel", null). Show ();
Break
Case R.ID.BUTTON4:
New Alertdialog.builder (this). Settitle ("check box"). Setmultichoiceitems (
New string[] {"Item1", "Item2"}, NULL, NULL)
. Setpositivebutton ("OK", NULL)
. Setnegativebutton ("Cancel", null). Show ();
Break
Case R.ID.BUTTON5:
New Alertdialog.builder (this). Settitle ("Radio Box"). SetIcon (
Android. R.drawable.ic_dialog_info). Setsinglechoiceitems (
New string[] {"Item1", "Item2"}, 0,
New Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog, int which) {
Dialog.dismiss ();
}
}). Setnegativebutton ("Cancel", null). Show ();
Break
Case R.ID.BUTTON6:
New Alertdialog.builder (this). Settitle ("list box"). Setitems (
New string[] {"Item1", "Item2"}, NULL). Setnegativebutton (
OK, null). Show ();
Break
Case R.ID.BUTTON7:
Layoutinflater inflater = Getlayoutinflater ();
View layout = Inflater.inflate (R.layout.demo,
(ViewGroup) Findviewbyid (R.id.dialog));
New Alertdialog.builder (this). Settitle ("Custom Layout"). Setview (Layout)
. Setpositivebutton ("OK", NULL)
. Setnegativebutton ("Cancel", null). Show ();
Break
Case R.ID.BUTTON8:
ProgressDialog dialog2 = new ProgressDialog (this);
Dialog2.settitle ("Progress dialog Box");
Dialog2.setmessage ("I am the Progress dialog box");
Dialog2.show ();
Break
Default
Break
}
}
private void Dialog () {
Builder = new Alertdialog.builder (mainactivity.this);
Builder.settitle ("hint");
Builder.seticon (R.drawable.ic_launcher);
Builder.setmessage ("Confirm the deletion?" ");
Builder.setpositivebutton ("confirm", null);
Builder.setnegativebutton ("Cancel", null);
}
private void Dialogg () {
dialog = new Alertdialog.builder (this)
. SetIcon (Android. R.drawable.btn_star)
. Settitle ("Survey of Preferences")
. Setmessage ("Do you like Jet Li's movies?") ")
. Setpositivebutton ("Very much like",
New Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog,
int which) {
TODO auto-generated Method Stub
Toast.maketext (Mainactivity.this, "I like his movie very much. ",
Toast.length_long). Show ();
}
})
. Setnegativebutton ("Don't like",
New Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog,
int which) {
TODO auto-generated Method Stub
Toast.maketext (Mainactivity.this, "I don't like his movies. ",
Toast.length_long). Show ();
}
})
. Setneutralbutton ("General", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Toast.maketext (Mainactivity.this, "I don't like it. ",
Toast.length_long). Show ();
}
}). Create ();
}
}
XML code:
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/zong"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<linearlayout
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
<button
Android:id= "@+id/button1"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Normal"/>
<button
Android:id= "@+id/button2"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Next three"/>
<button
Android:id= "@+id/button3"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "View"/>
<button
Android:id= "@+id/button4"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "check box"/>
<button
Android:id= "@+id/button5"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Radio Box"/>
<button
Android:id= "@+id/button6"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Form"/>
<button
Android:id= "@+id/button7"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Custom"/>
<button
Android:id= "@+id/button8"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Progress"/>
</LinearLayout>
</HorizontalScrollView>
android dialog box