Activities提供了一种方便管理的创建、保存、回复的对话框机制,例如 onCreateDialog(int)
, onPrepareDialog(int, Dialog)
, showDialog(int)
, dismissDialog(int)等方法,如果使用这些方法的话,Activity将通过
getOwnerActivity()方法返回该Activity管理的对话框(dialog).
1,提示框;
2,列表对话框
3,单选对话框
4,多选对话框
提示框,列表框,单选框,多选框写在一个java类中实现其效果
public class MainActivity extends Activity { private AlertDialog dialog; int selected, temp = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // 按钮的事件方法 public void show(View v) { // 获得被点击的按钮的id int id = v.getId(); switch (id) { case R.id.alert://提示框 this.showDialog(10); break; case R.id.list://列表框 this.showDialog(20); break; case R.id.radio://单选框 this.showDialog(30); break; case R.id.multi://多选框 this.showDialog(40); break; } } @Override protected Dialog onCreateDialog(int id, Bundle args) { switch (id) { case 10://提示框 // 创建一个用来构建对话框的对象 Builder build = new AlertDialog.Builder(this); build.setTitle("提示对话框"); build.setIcon(android.R.drawable.btn_dialog); build.setMessage("这里是提示的消息内容"); OnClickListener listener = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case DialogInterface.BUTTON_POSITIVE: Toast.makeText(MainActivity.this, "点击的是确定", Toast.LENGTH_SHORT).show(); break; case DialogInterface.BUTTON_NEGATIVE: Toast.makeText(MainActivity.this, "点击的取 消",Toast.LENGTH_SHORT).show(); break; case DialogInterface.BUTTON_NEUTRAL: Toast.makeText(MainActivity.this, "点击的是忽略", Toast.LENGTH_SHORT).show(); break; } } }; // 给对话框添加按钮 build.setPositiveButton("确定", listener); build.setNegativeButton("取消", listener); build.setNeutralButton("忽略", listener); AlertDialog dialog = build.create(); return dialog; case 20://列表框 Builder build1 = new AlertDialog.Builder(this); build1.setTitle("列表对话框"); build1.setIcon(android.R.drawable.btn_radio); final String[] items = { "打开", "关闭", "保存" }; OnClickListener listener1 = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "选中的是:" + items[which], Toast.LENGTH_SHORT).show(); } }; // 添加选项列表 build1.setItems(items, listener1); return build1.create(); case 30://单选框 Builder build2 = new AlertDialog.Builder(this); build2.setTitle("单选对话框"); build2.setIcon(android.R.drawable.btn_radio); final String[] items2 = { "在线", "隐身", "忙碌", "离线" }; OnClickListener listener2 = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (which >= 0) { temp = which; } else { if (which == DialogInterface.BUTTON_POSITIVE) { selected = temp; Toast.makeText(MainActivity.this,"选中的是:" +items2[selected], Toast.LENGTH_SHORT).show(); } else if (which == DialogInterface.BUTTON_NEGATIVE) { dialog.cancel(); } } } }; build2.setSingleChoiceItems(items2, selected, listener2); build2.setPositiveButton("确定", listener2); build2.setNegativeButton("取消", listener2); build2.setCancelable(false); return build2.create(); case 40://多选框 Builder build3 = new AlertDialog.Builder(this); build3.setTitle("多选对话框"); build3.setIcon(android.R.drawable.btn_radio); final String[] items3 = { "Java", "C++", "C#", "Swift" }; final boolean[] checkedItems = { true, true, false, false }; // 多选按钮的监听器 OnMultiChoiceClickListener listener33= new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkedItems[which] = isChecked; } }; build3.setMultiChoiceItems(items3, checkedItems, listener33); OnClickListener listener3 = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE) { String str = ""; for (int i = 0; i < items3.length; i++) { if (checkedItems[i]) { str += items3[i] + ","; } } Toast.makeText(MainActivity.this, "选中的是:" + str, Toast.LENGTH_SHORT).show(); } } }; build3.setPositiveButton("确定", listener3); build3.setCancelable(false); return build3.create(); } return null; } }
5,日期对话框
6,时间对话框
7,进度条对话框
8,进度条对话框
日期,时间,进度条对话框的代码实现;
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog); } // 按钮的方法 public void show(View v) { int id = v.getId(); switch (id) { case R.id.progressbtn: this.showDialog(10); break; case R.id.progress2: this.showDialog(20); break; case R.id.date: this.showDialog(30); break; case R.id.times: this.showDialog(40); break; } } // 对话框的方法 @Override protected Dialog onCreateDialog(int id, Bundle args) { switch (id) { case 10: // 进度条 ProgressDialog pdialog = new ProgressDialog(this); pdialog.setCancelable(false); pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pdialog.setMax(100); pdialog.setSecondaryProgress(30); return pdialog; case 20: ProgressDialog pdialog2 = new ProgressDialog(this); pdialog2.setTitle("对话框"); pdialog2.setMessage("正在下载.."); pdialog2.setCancelable(false); pdialog2.setProgressStyle(ProgressDialog.STYLE_SPINNER); return pdialog2; case 30: // 日期的监听器 OnDateSetListener callBack = new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(MainActivity.this, year + "" + monthOfYear + "" + dayOfMonth, Toast.LENGTH_SHORT).show(); } }; Calendar calender = Calendar.getInstance();// 获得系统的日期 可以将该方法申明在最外边 int year = calender.get(Calendar.YEAR); int month = calender.get(Calendar.MONTH); int day = calender.get(Calendar.DAY_OF_MONTH); DatePickerDialog dateDialog = new DatePickerDialog(this, callBack, year, month, day); return dateDialog; case 40: OnTimeSetListener callBacks = new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(MainActivity.this, hourOfDay + "" + minute, Toast.LENGTH_SHORT).show(); } }; Calendar calendertime = Calendar.getInstance();// 获得时间 int hour = calendertime.get(Calendar.HOUR_OF_DAY); int minute = calendertime.get(Calendar.MINUTE); TimePickerDialog timeDialog = new TimePickerDialog(this, callBacks, hour, minute, true); return timeDialog; } return null; } // 条用已经创建的对话框的方法 @Override protected void onPrepareDialog(int id, Dialog dialog, Bundle args) { switch (id) { case 10: ProgressDialog dialog2 = (ProgressDialog) dialog; new ProThread(dialog2).start(); break; case 20: ProgressDialog dialog3 = (ProgressDialog) dialog; new ProThread2(dialog3).start(); break; case 30: // 将对话框转成时间对话框 DatePickerDialog dialogdate = (DatePickerDialog) dialog; // 获得系统的日期时间 Calendar calender = Calendar.getInstance();// 获得系统的日期 // 获得日期 int year = calender.get(Calendar.YEAR); int month = calender.get(Calendar.MONTH); int day = calender.get(Calendar.DAY_OF_MONTH); // 还原系统的日期 dialogdate.getDatePicker().init(year, month, day, dialogdate); break; } } //系统默认的进度条 class ProThread2 extends Thread{ private ProgressDialog dialog; public ProThread2(ProgressDialog dialog) { this.dialog =dialog; } public void run(){ int num=0; while(num<20){ try { ++num; Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } dialog.dismiss();//消失; } } // 进度条的线程 class ProThread extends Thread { ProgressDialog dialog2; public ProThread(ProgressDialog dialog2) { this.dialog2 = dialog2; } public void run() { dialog2.incrementProgressBy(-dialog2.getProgress()); int max = dialog2.getMax(); while (dialog2.getProgress() < max) { try { dialog2.incrementProgressBy(1); Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } dialog2.dismiss();// 关闭对话框 } } }