Study-android 各种dialog的学习总结
2015-08-07 22:57
633 查看
package com.wyd.study.dialog; import com.wyd.study.R; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; /* *builder(生成器):Dialog的生产器配置 *Bundle(捆,束):Intent携带的信息 *handler(处理器):Message处理消息 * */ /* *Dialog对话框 * Known Direct Subclasses * AlertDialog, CharacterPickerDialog, Presentation * Known Indirect Subclasses * DatePickerDialog, ProgressDialog, TimePickerDialog * 下面主要介绍AlertDialog和ProgressDialog生成下面的几种 * 1、简单警告对话框 * 2、列表对话框 * 3、单选对话框 * 4、复选对话框 * 6、进度条 * 7、自定义对话框 * */ /* * ProgressDialog的说明 * ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog,实现DialogInterface接口 * ProgressDialog的创建方式有两种 * 一种是new Dialog * ProgressDialog dialog = new ProgressDialog(this); * dialog.show(); * 一种是调用Dialog的静态方法Dialog.show()。 * ProgressDialog dialog = ProgressDialog.show(this, "提示", "正在登陆中"); * * 方式一:new Dialog * ProgressDialog dialog = new ProgressDialog(this); * dialog.show(); * * 方式二:使用静态方式创建并显示,这种进度条只能是圆形条,设置title和Message提示内容 * ProgressDialog dialog = ProgressDialog.show(this, "提示", "正在登陆中"); * * 方式三 使用静态方式创建并显示,这种进度条只能是圆形条, * 这里最后一个参数boolean indeterminate设置是否是不明确的状态 * ProgressDialog dialog = ProgressDialog.show(this, "提示", "正在登陆中", false); * * 方式四 使用静态方式创建并显示,这种进度条只能是圆形条, * 这里最后一个参数boolean cancelable 设置是否进度条是可以取消的 * ProgressDialog dialog = ProgressDialog.show(this, "提示", "正在登陆中", false, true); * * 方式五 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数 * DialogInterface.OnCancelListener用于监听进度条被取消 * ProgressDialog dialog = ProgressDialog.show(this, "提示", "正在登陆中", true, true, cancelListener); * * private OnCancelListener cancelListener = new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { Toast.makeText(MainActivity.this, "进度条被取消", Toast.LENGTH_LONG).show(); } }; * * */ /* ProgressDialog的使用步骤 ● 标题的设置,调用setTitle()方法。 ● 消息的设置,调用setMessage()方法。 ● 在确定(indeterminate=false)的进度条对话框里,进度最大值的设置,调用setMax()方法。 ● 当前进度值的设置,调用setProgress()方法。 ● 第2个进度值的设置,调用setSecondaryProgress()方法 ● 当前进度值的设置,调用incrementProgressBy()方法 ● 第2进度值的增减,调用incrementSecondaryProgressBy()方法 ● 进度对话框风格的设置,调用setProgressStyle()方法。 ProgressDialog.STYLE_SPINNER 旋体进度条风格 ProgressDialog.STYLE_HORIZONTAL 横向进度条风格 ● 默认风格是ProgressDialog.STYLE_SPINNER旋体进度条风格 ● 取消按钮的设置,调用setCancelable()方法。 ● 进度对话框的表示,调用show()方法。 */ public class DialogActivity extends Activity implements OnClickListener { private Button btnSimpleAlertDialog = null; private Button btnListAlertDialog = null; private Button btnSingleChoiceAlertDialog = null; private Button btnMultipleChoiceAlertDialog = null; private Button btnSpinnerProgressAlertDialog = null; private Button btnHorizonalProgressAlertDialog = null; private Button btnCustomAlertDialog = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dialog); btnSimpleAlertDialog = (Button) findViewById(R.id.simpleAlertDialog); btnListAlertDialog = (Button) findViewById(R.id.listAlertDialog); btnSingleChoiceAlertDialog = (Button) findViewById(R.id.singleChoiceAlertDialog); btnMultipleChoiceAlertDialog = (Button) findViewById(R.id.multipleChoiceAlertDialog); btnSpinnerProgressAlertDialog = (Button) findViewById(R.id.spinnerProgressAlertDialog); btnHorizonalProgressAlertDialog = (Button) findViewById(R.id.horizonalProgressAlertDialog); btnCustomAlertDialog = (Button) findViewById(R.id.customAlertDialog); btnSimpleAlertDialog.setOnClickListener(this); btnListAlertDialog.setOnClickListener(this); btnSingleChoiceAlertDialog.setOnClickListener(this); btnMultipleChoiceAlertDialog.setOnClickListener(this); btnSpinnerProgressAlertDialog.setOnClickListener(this); btnHorizonalProgressAlertDialog.setOnClickListener(this); btnCustomAlertDialog.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.simpleAlertDialog: new AlertDialog.Builder(this).setTitle("确认") .setView(new EditText(this)).setMessage("确定吗?") .setIcon(android.R.drawable.ic_dialog_info) .setPositiveButton("是", null)// null是DialogInterface.OnClickListener来处理按下监听 .setNegativeButton("否", null).show(); break; case R.id.listAlertDialog: ImageView img = new ImageView(this); // 想要设置图片的时候需要这样做 img.setImageResource(R.drawable.ic_launcher); new AlertDialog.Builder(this) .setView(img)// 跟程序先后没有关系,编辑框和图片都是在最下面 .setTitle("列表框") .setItems(new String[] { "列表项1", "列表项2", "列表项3" }, null) .setNegativeButton("确定", null).show(); break; case R.id.singleChoiceAlertDialog: new AlertDialog.Builder(this) .setTitle("请选择") .setIcon(android.R.drawable.ic_dialog_info) .setSingleChoiceItems( new String[] { "选项1", "选项2", "选项3", "选项4" }, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).setPositiveButton("确认", null) .setNegativeButton("取消", null).show(); break; case R.id.multipleChoiceAlertDialog: new AlertDialog.Builder(this) .setTitle("多选框") .setMultiChoiceItems( new String[] { "选项1", "选项2", "选项3", "选项4" }, null, null).setPositiveButton("确定", null) .setNegativeButton("取消", null).show(); break; case R.id.customAlertDialog: LayoutInflater factory = LayoutInflater.from(this); final View textView = factory.inflate(R.layout.dialog_custom, null); new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher) // 设置图标 .setTitle("自定义对话框").setView(textView) .setPositiveButton("确定", null)// 这个函数是AlertDialog有的 .setNegativeButton("取消", null)// 并不是ProgressDialog有的 .setNeutralButton("中立", null).show(); break; case R.id.spinnerProgressAlertDialog: // ProgressDialog dialog = new ProgressDialog(this); // dialog.show(); // ProgressDialog.show(this, "提示", "正在登陆中");//静态方式,返回键不能退出 // ProgressDialog.show(this, "提示", "正在登陆中", false);//返回键不能退出 // ProgressDialog.show(this, "提示", "正在登陆中", false, true);//返回键能退出 // ProgressDialog.show(this, "提示", "正在登陆中", true,true, null); // //返回键能退出//OnCancelListener final ProgressDialog dialog = new ProgressDialog(this); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条 dialog.setCancelable(true);// 设置是否可以通过点击Back键取消 dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条 dialog.setIcon(R.drawable.ic_launcher);// // 设置提示的title的图标,默认是没有的,如果没有设置title的话只设置Icon是不会显示图标的 dialog.setTitle("提示"); // dismiss监听 dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { Toast.makeText(getApplicationContext(), "进度条dismiss", Toast.LENGTH_SHORT).show(); } }); // 监听Key事件被传递给dialog dialog.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { return false; } }); // 监听cancel事件 dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // Toast.makeText(getApplicationContext(), // "进度条cancel",Toast.LENGTH_SHORT).show(); } }); // 设置可点击的按钮,最多有三个(默认情况下) dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.setMessage("这是一个圆形进度条"); dialog.show(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(5000); // cancel和dismiss方法本质都是一样的,都是从屏幕中删除Dialog,唯一的区别是 // 调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话, // dismiss方法不会回掉,实验结果是也会回调 // dialog.cancel(); dialog.dismiss(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); bre a523 ak; case R.id.horizonalProgressAlertDialog: final ProgressDialog dialog1 = new ProgressDialog(this); dialog1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置水平进度条 dialog1.setCancelable(true);// 设置是否可以通过点击Back键取消 dialog1.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条 dialog1.setIcon(R.drawable.ic_launcher);// 设置提示的title的图标,默认是没有的 dialog1.setTitle("提示"); dialog1.setMax(100); dialog1.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog1.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog1.setButton(DialogInterface.BUTTON_NEUTRAL, "中立", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog1.setMessage("这是一个水平进度条"); dialog1.show(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub int i = 0; while (i < 100) { try { Thread.sleep(200); // 更新进度条的进度,可以在子线程中更新进度条进度 dialog1.incrementProgressBy(1); dialog1.incrementSecondaryProgressBy(10);// 二级进度条更新方式 i++; } catch (Exception e) { // TODO: handle exception } } // 在进度条走完时删除Dialog dialog1.dismiss(); } }).start(); break; default: break; } } }
源码下载:http://download.csdn.net/detail/yidong_wang/8975697
相关文章推荐
- Android:view常用属性和操作方法
- Android应用:自定义SurfaceView
- android 常用
- 浅谈Android Fragment嵌套使用存在的一些BUG以及解决方法
- Android指南针之加速度传感器地磁传感器-android学习之旅(67)
- Android指南针之加速度传感器地磁传感器-android学习之旅(67)
- Android指南针之加速度传感器地磁传感器-android学习之旅(67)
- Android指南针之加速度传感器地磁传感器-android学习之旅(67)
- Android ListView&异步加载的学习(四)——完善ListView滑动效率问题
- Android(java)学习笔记144:Android音视频录制类MediaRecorder用法举例
- Android Pitfall - 扒一扒RadioGroup 和 RadioButton
- Android菜单menu控件大全
- android常见错误汇总
- 解决Android真机设备调试时LogCat的日志无法输出的问题
- 浅析android 四大布局
- android 开发者选项
- Android的加速度传感器模拟摇一摇的效果-android学习之旅(66)
- Android的加速度传感器模拟摇一摇的效果-android学习之旅(66)
- Android的加速度传感器模拟摇一摇的效果-android学习之旅(66)
- Android的加速度传感器模拟摇一摇的效果-android学习之旅(66)