1.简单的提示框.
public void CreateDialog(String title, String content){ Dialog alertDialog = new AlertDialog.Builder(this). setTitle(title). // 设置提示框的标题 setMessage(content). //设置提示框的内容 setIcon(R.drawable.ic_launcher). //设置提示框的图标 create(); alertDialog.show(); }
运行效果:
2. 带有确定和取消按钮的对话框.
/** * create a dialog with 2 button(Ok && Cancle) * @author Andy */ public void CreateDialogs(){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("你确定要离开吗?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //这里添加点击确定后的逻辑 showDialog("你选择了确定"); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //这里添加点击确定后的逻辑 showDialog("你选择了取消"); } }); builder.create().show(); } /** * alertDialog show the message after user click button * @param content * @author Andy */ private void showDialog(String content) { Dialog alertDialog = new AlertDialog.Builder(this). setTitle("Test"). setMessage(content). setIcon(R.drawable.ic_launcher). create(); alertDialog.show(); }
运行效果:
3.带有多个按钮的对话框.
/** * create a dialog with 3 button * @author Andy */ public void CreateDialogs(){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("投票"); builder.setMessage("您认为什么样的内容能吸引您?"); builder.setPositiveButton("有趣味的", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { showDialog("你选择了有趣味的"); } }); builder.setNeutralButton("有思想的", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { showDialog("你选择了有思想的"); } }); builder.setNegativeButton("主题强的", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { showDialog("你选择了主题强的"); } }); builder.create().show(); }
运行效果:
4.列表框:
/** * create a list box * @author Andy */ public void CreateDialogs(){ final String[] Items = {"item0","item1","itme2","item3","itme4","item5","item6"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("列表选择框"); builder.setItems(Items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //点击后弹出窗口选择了第几项 showDialog("你选择的id为" + which + " , " + Items[which]); } }); builder.create().show(); }
运行效果:
5.单选列表框.
/** * create a single select list box * @author Andy */ private int mSingleChoiceID = -1; public void CreateDialogs(){ final String[] Items = {"item0","item1","itme2","item3","itme4","item5","item6"}; mSingleChoiceID = -1; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("单项选择"); builder.setSingleChoiceItems(Items, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { mSingleChoiceID = whichButton; showDialog("你选择的id为" + whichButton + " , " + Items[whichButton]); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { if(mSingleChoiceID > 0) { showDialog("你选择的是" + mSingleChoiceID); } } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { showDialog("你点击了取消按钮"); } }); builder.create().show(); }
运行效果:
6.多选列表框:
/** * create a Multiple choice list box * @author Andy */ ArrayList<Integer> MultiChoiceID = new ArrayList<Integer>(); public void CreateDialogs(){ MultiChoiceID.clear(); final String[] Items = {"item0","item1","itme2","item3","itme4","item5","item6"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("多项选择"); builder.setMultiChoiceItems(Items, new boolean[]{false, false, false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) { if(isChecked) { MultiChoiceID.add(whichButton); showDialog("你选择的id为" + whichButton + " , " + Items[whichButton]); }else { MultiChoiceID.remove(whichButton); } } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String str = ""; int size = MultiChoiceID.size(); for (int i = 0 ;i < size; i++) { str += Items[MultiChoiceID.get(i)] + ", "; } showDialog("你选择的是" + str); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { showDialog("你点击了取消按钮"); } }); builder.create().show(); }
运行效果:
7.进度条.
点击进度条框按钮后 开启一个线程计算读取的进度 假设读取结束为 100.
Progress在小于100的时候一直在线程中做循环++ 只到读取结束后,停止线程。
/** * create a ProgressDialog * @author Andy */ public void CreateDialogs(){ final ProgressDialog proDialog = android.app.ProgressDialog.show(MainActivity.this, "测试", "等待2秒!"); Thread thread = new Thread() { public void run() { try { sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } proDialog.dismiss();//必须在线程结束时调用对话框对象的dismiss()方法,否则程序将进入死循环当中 } }; thread.start(); }
运行效果: