Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 531|回复: 0

[实例教程]Android学习笔记-Dialog

[复制链接]

该用户从未签到

发表于 2011-10-22 13:00:55 | 显示全部楼层 |阅读模式
Dialog的分类:
l  AlertDialog
l  ProgressDialog
l  DatePickerDialog:供用户选择日期
l  TimerPickerDialog:供用户选择时间
一、显示Dialog创建dialog需在activity的类中实现onCreateDialog(int)方法。如果dialog的定义未写在该方法中时,此dialog将不绑定到任何activity,用setOwnerActivity(Activity)来邦定activity。
onCreateDialog(int)在dialog创建时调用,适合做dialog的初始化工作。
onPrepareDialog(int,Dialog)在dialog已经创建,每次显示前执行,可以在每次dialog打开前做属性的修改。
showDialog(ID)来显示Dialog。
二、关闭Dialogdismiss()ialog主动关闭。
dismissDialog(ID):从activity中关闭。
removeDialog(ID):清空。
如果Dialog是通过onCreatDialog()创建管理的话,每次dismiss后Dialog的对象状态会被Activity保存,如果不想让它保存,或者需要清空state的话需要用removeDialog(int)。
2.1使用dismiss监听器如果希望dialog dismiss时执行一些操作的话,需要为Dialog邦定监听器。
需要实现接口DialogInterface.OnDismissListener,然后把对OnDismissListener的实现传递给setOnDismissListener().当Dialog被Cancelled的话,该OnDismissListener监听器也会检测到,但如果想明确定义Dialog被cancelled(不正常dismiss)的话需要定义DialogInterface.OnCancelListener with setOnCancelListener().
三、创建AlertDialogAlertDialog具有如下特征:
l  A title
l  A text message
l  One, two, or three buttons

l  A list of selectable items (with optional checkboxes or radiobuttons)
使用AlertDialog.Builder子类创建AlertDialog。
示例代码如下:
AlertDialog.Builderbuilder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure youwant to exit?")
.setCancelable(false)
      .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id) {
MyActivity.this.finish();
          }
      })
      .setNegativeButton("No", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
          }
      });
AlertDialog alert = builder.create();
在需要显示AlertDialog的地方使用showDialog(int)调用,系统将转到onCreateDialog(int)执行,并返回Dialog;
此外我们还可以为AlertDialog添加setItem,setSingleChoiceItems等。
四、创建ProgressDialog
通过调用ProgressDialog.show()方法启动ProgressDialog,比如在onCreateDialog()中创建ProgressDialog示例代码:
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true);

4.1显示progressBar示例代码:
ProgressDialogprogressDialog;
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
一般progressBar的使用在额外的线程中
五、创建自定义Dialog
我们可以自定义Dialog的布局控件,通过setContentView(view)将定义的资源文件传给Dialog对象。
示例代码如下:
1、  定义资源文件custom_dialog.xml:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
androidrientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<ImageViewandroid:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
               />
<TextViewandroid:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
              />
</LinearLayout>
2、  将资源文件传给Dialog对象:
ContextmContext = getApplicationContext();
Dialogdialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("CustomDialog");

TextViewtext = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello,this is a custom dialog!");
ImageViewimage = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
3、使用showDialog显示Dialog
注意:baseDialog的title是必须的,如果不想使用title的话应该用AlertDialog。AlertDialog不再使用setContentView而是使用setView(View),因此需要从XML中来扩展view。
实例如下:
AlertDialog.Builderbuilder;
AlertDialogalertDialog;

Context mContext = getApplicationContext();
LayoutInflaterinflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
                               (ViewGroup)findViewById(R.id.layout_root));

TextView text = (TextView)layout.findViewById(R.id.text);
text.setText("Hello, this is a customdialog!");
ImageView image = (ImageView)layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

builder = newAlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-6-14 16:48 , Processed in 0.357542 second(s), 34 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表