Package com. zxing. dialog;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. app. Dialog;
Import android. content. Context;
Import android. content. DialogInterface;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Public class DialogTest extends Activity {
Private static final int DIALOG = 1;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button btn = (Button) findViewById (R. id. button1 );
Btn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
ShowDialog (DIALOG); // the function in the Activity, then call the onCreateDialog method, and finally call the onPrepareDialog method}
});
}
@ Override
Protected Dialog onCreateDialog (int id ){
// TODO Auto-generated method stub
If (id = 1 ){
Return this. buildDialog (DialogTest. this );
} Else {
Return null;
}
}
@ Override
Protected void onPrepareDialog (int id, Dialog dialog ){
// TODO Auto-generated method stub
Super. onPrepareDialog (DIALOG, dialog );
}
Private Dialog buildDialog (Context context ){
AlertDialog. Builder builder = new AlertDialog. Builder (context );
Builder. setIcon (R. drawable. alert_dialog_icon );
Builder. setTitle (R. string. alertitle );
Builder. setPositiveButton (R. string. right, new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
SetTitle ("You have clicked the correct button above the dialog box ");
}
});
Return builder. create ();
}
}
Author: MR. zxing