Simple code for the Android pop-up dialog box

Source: Internet
Author: User

 

 

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

Related Article

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.