Show dialog box window
1. Create a Dialog1 project and add a Button in the activity_main.xml file:
2. Create a dialog box in the MainActivity. java file. Add the following code:
Package com. example. dialog; import android. OS. bundle; import android. app. activity; import android. app. alertDialog; import android. app. dialog; import android. content. dialogInterface; import android. view. menu; import android. view. view; import android. widget. toast; public class MainActivity extends Activity {CharSequence [] items = {"Google", "Apple", "Microsoft"}; boolean [] itemsChecked = new boolean [items. le Ngth]; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ;}@ SuppressWarnings ("deprecation") public void onClick (View v) {showDialog (0); // a dialog box is displayed when you click the button. This method accepts an integer parameter, used to identify the specific dialog box to be displayed (only a dialog box marked as 0 is created here ).} @ Override @ Deprecated // onCreateDialog () is a callback method used to create a dialog box managed by the activity. This callback method is called when showDialog () is called. Protected Dialog onCreateDialog (int id) {// TODO Auto-generated method stubswitch (id) {case 0: // create a Dialog box, you must use the Builder constructor of the AlertDialog class to set different attributes, such as labels, titles, buttons, and check boxes. Return new AlertDialog. Builder (this) // call the Builder constructor of the AlertDialog object .. SetIcon (R. drawable. ic_launcher ). setTitle ("This is a dialog with some simple text... "). setPositiveButton ("OK", // set OK button new DialogInterface. onClickListener () {// set the Click Event @ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubToast. makeText (getBaseContext (), "OK clicked! ", Toast. LENGTH_SHORT ). show ();}}). setNegativeButton ("Cancel", // set the Cancel button to new DialogInterface. onClickListener () {// set the Click Event @ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubToast. makeText (getBaseContext (), "Cancel clicked! ", Toast. LENGTH_SHORT ). show ();}}). setMultiChoiceItems (items, itemsChecked, // set the check box new DialogInterface. onMultiChoiceClickListener () {// set the Click Event @ Overridepublic void onClick (DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stubToast. makeText (getBaseContext (), items [which] // use the input parameter isChecked to check whether the message is returned. + (IsChecked? "Checked! ":" Unchecked! "), Toast. LENGTH_SHORT ). show ();}}). create ();} return null;} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
3. Run the command, as shown in:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + teo797c0xawjuw.vcd4kpha + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140507/20140507091122215.jpg" alt = "">