Reference https://www.cnblogs.com/wanghaoyuhappy/p/5292815.html
1. Ordinary dialog
1 public void click01(View view){ 2 AlertDialog.Builder builder = new AlertDialog.Builder(this);//内部类 3 builder.setTitle ( "Tips"); 4 builder.setMessage ( "Are you sure you want to delete it?"); 5 // OK button 6 builder.setPositiveButton("确定", new OnClickListener() { 7 8 @Override 9 public void onClick(DialogInterface dialog, int which) { 10 // OK to delete the code 11 Toast.makeText (MainActivity.this, "deleted successfully", 0) .show (); 12 } 13 }); 14 // point cancel button 15 builder.setNegativeButton("取消", null); 16 17 // display a dialog box 18 //AlertDialog dialog = builder.create(); 19 //dialog.show(); 20 // or simply point 21 builder.show(); 22 }
Show results:
2. radio Dialog
1 public void click02(View view){ 2 AlertDialog.Builder builder = new AlertDialog.Builder(this); 3 4 final String [] items = { "M", "F", "Others"}; 5 6 builder.setSingleChoiceItems(items, 0, new OnClickListener() { 7 8 @Override 9 public void onClick(DialogInterface dialog, int which) { 10 String item = items[which]; 11 Toast.makeText (MainActivity.this, "Your choice is:" + item, 0) .show (); 12 } 13 }); 14 15 // display a dialog box 16 builder.show(); 17 }
Show results:
3. multiple choice dialog
public void click03(View view){ AlertDialog.Builder builder = new AlertDialog.Builder(this); final String [] items = { "basketball", "table tennis", "badminton", "billiards", "glass beads"}; boolean[] checkedItems = {true,false,false,true,false}; builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { String item = items[which]; Toast.makeText (MainActivity.this, "select your hobby is:" + item + ", status is:" + isChecked, 0) .show (); } }); //display builder.show(); }
Show results:
4. A dialog box with no progress bar
1 public void click04(View view){ 2 final ProgressDialog pd = ProgressDialog.show (this, "without the progress bar", "a beautiful large wave hit"); 3 new Thread(){ 4 public void run() { 5 SystemClock.sleep(2000); 6 pd.dismiss(); 7 }; 8 }.start(); 9 }
Show results:
5. A dialog box with a progress bar
1 public void click05(View view){ 2 final ProgressDialog pd = new ProgressDialog(this); 3 // Set the level of progress bar style 4 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 5 pd.setMax(100); 6 pd.show(); 7 new Thread(){ 8 public void run() { 9 for (int i = 0; i < 100; i++) { 10 SystemClock.sleep(50); 11 pd.setProgress(i); 12 } 13 pd.dismiss(); 14 }; 15 }.start(); 16 }
Show results: