android学习日记27--Dialog使用及其设计模式
2014-04-26 10:34
561 查看
1、Dialog概述
对话框一般是一个出现在当前Activity之上的一个小窗口,处于下面的Activity失去焦点, 对话框接受所有的用户交互。
对话框一般用于提示信息和与当前应用程序直接相关的小功能。
2、Dialog 分类:
警告对话框 AlertDialog : 一个可以有0到3个按钮, 一个单选框或复选框的列表的对话框. 警告对话框可以创建大多数的交互界面, 是推荐的类型。
进度对话框 ProgressDialog: 显示一个进度环或者一个进度条. 由于它是 AlertDialog 的扩展, 所以它也支持按钮。
日期选择对话框 ProgressDialog : 让用户选择一个日期。
时间选择对话框 TimePickerDialog : 让用户选择一个时间。
3、Dialog应用
a、AlertDialog一般用法:
取得创建者的类,AlertDialog.Builder builder = new Builder(Context);然后通过builder.setXX一系列方法来设置属性;
最后builder.create().show()来显示Dialog。
b、ProgressDialog、ProgressDialog、TimePickerDialog 用法:
有些区别,是直接 new XXDialog(Context); 然后通过实例化的dialog.setXX设置属性;最后直接dialog.show()展示。
c、代码实例
实现如下9中Dialog
![](http://images.cnitblog.com/i/602984/201404/261029257169969.png)
按钮就不一一点进去演示,直接看源码:
其中自定义Dialog的布局文件test.xml
4、Dialog设计模式
Dialog设计模式中有使用建造者模式,建造者模式将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
Android中AlertDialog是一个多面手,可以有着不同的样式和呈现,这样通过Builder就可以有效实现构建和表示的分离。
AlertDialog.Builder就是具体建造者,另外,它是以匿名类的方式被创建的,而且,Builder类是AlertDialog的内部类,这样,
耦合性比较低,这正是面向对象中要达到的设计意图之一。 最后调用show函数,它的返回类型正是我们要创建的产品,即AlertDialog。
所以,Builder(具体建造者)是AlertDialog(产品)的内部匿名类,用来创建并显示一个dialog。
对话框一般是一个出现在当前Activity之上的一个小窗口,处于下面的Activity失去焦点, 对话框接受所有的用户交互。
对话框一般用于提示信息和与当前应用程序直接相关的小功能。
2、Dialog 分类:
警告对话框 AlertDialog : 一个可以有0到3个按钮, 一个单选框或复选框的列表的对话框. 警告对话框可以创建大多数的交互界面, 是推荐的类型。
进度对话框 ProgressDialog: 显示一个进度环或者一个进度条. 由于它是 AlertDialog 的扩展, 所以它也支持按钮。
日期选择对话框 ProgressDialog : 让用户选择一个日期。
时间选择对话框 TimePickerDialog : 让用户选择一个时间。
3、Dialog应用
a、AlertDialog一般用法:
取得创建者的类,AlertDialog.Builder builder = new Builder(Context);然后通过builder.setXX一系列方法来设置属性;
最后builder.create().show()来显示Dialog。
b、ProgressDialog、ProgressDialog、TimePickerDialog 用法:
有些区别,是直接 new XXDialog(Context); 然后通过实例化的dialog.setXX设置属性;最后直接dialog.show()展示。
c、代码实例
实现如下9中Dialog
![](http://images.cnitblog.com/i/602984/201404/261029257169969.png)
按钮就不一一点进去演示,直接看源码:
/**多个按钮信息框 **/ private static final int DIALOG_1 = 2; /**列表框 **/ private static final int DIALOG_2 = 3; /**进度条框 **/ private static final int DIALOG_3 = 4; /**单项选择列表框 **/ private static final int DIALOG_4 = 5; /**多项选择列表框 **/ private static final int DIALOG_5 = 6; /**自定义布局 **/ private static final int DIALOG_6 = 7; /**读取进度框 **/ private static final int DIALOG_7 = 8; /**自定义布局 **/ private static final int DIALOG_8 = 9; /**读取进度框 **/ private static final int DIALOG_9 = 10; private ProgressDialog pDialog; private DatePickerDialog dDialog; private TimePickerDialog tDialog; private Calendar c; final String[] items = {"item0","item1","itme2","item3","itme4"}; ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { CreatDialog(DIALOG_1); } }); Button button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener() { public void onClick(View v) { CreatDialog(DIALOG_2); } }); Button button3 = (Button) findViewById(R.id.button3); button3.setOnClickListener(new OnClickListener() { public void onClick(View v) { CreatDialog(DIALOG_3); //mProgressDialog.setProgress(0); } }); Button button4 = (Button) findViewById(R.id.button4); button4.setOnClickListener(new OnClickListener() { public void onClick(View v) { CreatDialog(DIALOG_4); } }); Button button5 = (Button) findViewById(R.id.button5); button5.setOnClickListener(new OnClickListener() { public void onClick(View v) { CreatDialog(DIALOG_5); } }); Button button6 = (Button) findViewById(R.id.button6); button6.setOnClickListener(new OnClickListener() { public void onClick(View v) { CreatDialog(DIALOG_6); } }); Button button7 = (Button) findViewById(R.id.button7); button7.setOnClickListener(new OnClickListener() { public void onClick(View v) { CreatDialog(DIALOG_7); } }); Button button8 = (Button) findViewById(R.id.button8); button8.setOnClickListener(new OnClickListener() { public void onClick(View v) { CreatDialog(DIALOG_8); } }); Button button9 = (Button) findViewById(R.id.button9); button9.setOnClickListener(new OnClickListener() { public void onClick(View v) { CreatDialog(DIALOG_9); } }); } public void CreatDialog(int id) { AlertDialog.Builder builder = new Builder(DialogDemoActivity.this); switch (id) { case DIALOG_1: builder.setIcon(R.drawable.ic_launcher); builder.setTitle("投票"); builder.setMessage("您认为什么样的内容吸引你?"); builder.setPositiveButton("有趣的", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub showDialog("您选择了有趣的!"); } }); builder.setNeutralButton("有内涵的", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub showDialog("您选择了有内涵的!"); } }); builder.setNegativeButton("其他", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub showDialog("您选择了其他!"); } }); break; case DIALOG_2: builder.setTitle("列表框"); builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub showDialog("您选择了"+items[which]); } }); break; case DIALOG_3: pDialog = new ProgressDialog(DialogDemoActivity.this); pDialog.setIcon(R.drawable.ic_launcher); pDialog.setTitle("带进度条的"); pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pDialog.setMax(100); pDialog.setButton("ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); pDialog.setButton2("cancle", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); pDialog.show(); new Thread(this).start(); return; case DIALOG_4: builder.setTitle("单列表选择框"); builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub showDialog("你选择的id为" + which + " , " + items[which]); } }); break; case DIALOG_5: MultiChoiceID.clear(); builder.setTitle("多列表选择框"); builder.setMultiChoiceItems(items , new boolean[]{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) { } }); break; case DIALOG_6: builder.setTitle("自定义对话框"); LayoutInflater layoutInflater = LayoutInflater.from(this); final View layout = layoutInflater.inflate(R.layout.test, null); builder.setView(layout); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { EditText userName = (EditText) layout.findViewById(R.id.etUserName); EditText password = (EditText) layout.findViewById(R.id.etPassWord); showDialog("姓名 :" + userName.getText().toString() + "密码:" + password.getText().toString() ); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }); break; case DIALOG_7: pDialog = new ProgressDialog(DialogDemoActivity.this); pDialog.setIcon(R.drawable.ic_launcher); pDialog.setTitle("循环进度"); pDialog.setMessage("正在读取"); pDialog.setIndeterminate(true); // 设置进度条不明确,即一直在滚动,不清楚进度 pDialog.setCancelable(true); // 设置 返回键 是否取消 进度框 pDialog.show(); return; case DIALOG_8: c= Calendar.getInstance(); dDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub } }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)); dDialog.show(); return; case DIALOG_9: c= Calendar.getInstance(); tDialog = new TimePickerDialog( //创建TimePickerDialog对象 this, new TimePickerDialog.OnTimeSetListener(){ //创建OnTimeSetListener监听器 @Override public void onTimeSet(TimePicker tp, int hourOfDay, int minute) { } }, c.get(Calendar.HOUR_OF_DAY), //传入当前小时数 c.get(Calendar.MINUTE), //传入当前分钟数 false ); tDialog.show(); return; } builder.create().show(); } private void showDialog(String str) { new AlertDialog.Builder(DialogDemoActivity.this) .setMessage(str) .show(); } @Override public void run() { int Progress = 0; while(Progress < 100) { try { Thread.sleep(100); Progress++; pDialog.incrementProgressBy(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
其中自定义Dialog的布局文件test.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal" android:id="@+id/dialog"> <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal" android:id="@+id/dialogname"> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tvUserName" android:text="姓名:" /> <EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/etUserName" android:minWidth="200dip"/> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal" android:id="@+id/dialognum" android:layout_below="@+id/dialogname" > <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tvPassWord" android:text="密码:" /> <EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/etPassWord" android:minWidth="200dip"/> </LinearLayout> </RelativeLayout>
4、Dialog设计模式
Dialog设计模式中有使用建造者模式,建造者模式将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
Android中AlertDialog是一个多面手,可以有着不同的样式和呈现,这样通过Builder就可以有效实现构建和表示的分离。
AlertDialog.Builder就是具体建造者,另外,它是以匿名类的方式被创建的,而且,Builder类是AlertDialog的内部类,这样,
耦合性比较低,这正是面向对象中要达到的设计意图之一。 最后调用show函数,它的返回类型正是我们要创建的产品,即AlertDialog。
所以,Builder(具体建造者)是AlertDialog(产品)的内部匿名类,用来创建并显示一个dialog。
相关文章推荐
- Android设计模式学习日记04--单例模式
- Android设计模式学习日记05--模板模式
- 设计模式实例学习-单例模式(Android中的使用场景)
- Android设计模式学习日记03--观察者模式
- android学习日记28--Android中常用设计模式总结
- OpenCV 2 学习笔记(13): 算法的基本设计模式<4> :使用Model-View-Controller模式创建一个应用程序
- 【笨鸟先飞】Java重新学习日记20--设计模式之适配器模式
- Android中采用MVP设计模式和RxJava的情况下,使用BroadcastReceiver
- java/android 设计模式学习笔记(11)---原型模式
- 设计模式学习日记二(持续更新)
- Android经常使用设计模式(二)
- Android代码中使用的设计模式思想
- ANdroid设计模式学习(一)面向对象的六大原则
- 设计模式学习日记二(持续更新)
- 学习Android中设计模式的笔记和总结(一)
- Android 异步任务:AsyncTask 学习解析及基本使用 (Android单线程模式)
- Android使用Builder模式自定义Dialog
- Android设计模式学习笔记(原型模式)
- 学习日记-责任链设计模式
- Android应用开发中控制反转IoC设计模式使用教程