第十七讲:对话框 Android Dialog
03 Aug 本讲内容:Android Dialog 对话框
1、Dialog介绍
2、AlertDialog的基本使用
3、自定义对话框 Custom Dialog
一、Dialog介绍
Dialog也是Android中常用的用户界面元素,他同Menu一样也不是View的子类。让我们看一下它的继承关系:
这里要留意一下他的直接子类 AlertDialog,和间接子类 DatePickerDialog,ProgressDialog,TimePickerDialog,其中后三个我们在前面的章节已经讲过,今天我们把重点放在AlertDialog上。
二、AlertDialog的基本使用
AlertDialog对话框是Dialog的子类,它提供一个图标,一个标题,一个文本和3个按钮。我们在Activity里可以自行创建和显示Dialog,也可以通过Activity的方法对其进行管理。我们可以通过下面的例子学习它的使用方法,同样请注意代码中的注释。
1、创建一个项目 Lesson17_HelloAlertDialog,Activity的文件名叫 MainHelloAlertDialog.java
2、res/layout/main.xml 的内容如下:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < linearlayout android:layout_height = "fill_parent" android:layout_width = "fill_parent" android:orientation = "vertical" xmlns:android = "http://schemas.android.com/apk/res/android" > |
04 |
< textview android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:id = "@+id/TextView01" android:text = "对话框示例" android:textsize = "20sp" android:layout_margintop = "5dp" > |
07 |
< button android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:id = "@+id/Button01" android:text = "显示对话框|ShowDialog()" android:textsize = "20sp" android:layout_margintop = "5dp" > |
10 |
< button android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:id = "@+id/Button02" android:text = "关闭对话框|dismissDialog()" android:textsize = "20sp" android:layout_margintop = "5dp" > |
13 |
< button android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:id = "@+id/Button03" android:text = "移除对话框|removeDialog()" android:textsize = "20sp" android:layout_margintop = "5dp" > |
3、MainHelloAlertDialog.java的内容如下:
001 | package android.basic.lesson17; |
003 | import android.app.Activity; |
004 | import android.app.AlertDialog; |
005 | import android.app.Dialog; |
006 | import android.content.DialogInterface; |
007 | import android.content.DialogInterface.OnClickListener; |
008 | import android.os.Bundle; |
009 | import android.view.View; |
010 | import android.widget.Button; |
011 | import android.widget.Toast; |
013 | public class MainHelloAlertDialog extends Activity { |
016 |
int Edward_Movie_Dialog = 1 ; |
019 |
OnClickListener ocl = new OnClickListener() { |
021 |
public void onClick(DialogInterface dialog, int which) { |
023 |
case Dialog.BUTTON_NEGATIVE: |
024 |
Toast.makeText(MainHelloAlertDialog. this , "我不喜欢他的电影。" , |
025 |
Toast.LENGTH_LONG).show(); |
027 |
case Dialog.BUTTON_NEUTRAL: |
028 |
Toast.makeText(MainHelloAlertDialog. this , "说不上喜欢不喜欢。" , |
029 |
Toast.LENGTH_LONG).show(); |
031 |
case Dialog.BUTTON_POSITIVE: |
032 |
Toast.makeText(MainHelloAlertDialog. this , "我很喜欢他的电影。" , |
033 |
Toast.LENGTH_LONG).show(); |
040 |
/** Called when the activity is first created. */ |
041 |
public void onCreate(Bundle savedInstanceState) { |
042 |
super .onCreate(savedInstanceState); |
043 |
setContentView(R.layout.main); |
046 |
Dialog dialog = new AlertDialog.Builder( this ) |
047 |
.setIcon(android.R.drawable.btn_star).setTitle( "喜好调查" ) |
048 |
.setMessage( "你喜欢看爱德华.诺顿Edward Norton的电影吗?" ) |
049 |
.setNegativeButton( "不喜欢" , ocl).setNeutralButton( "一般般" , ocl) |
050 |
.setPositiveButton( "很喜欢" , ocl).create(); |
056 |
Button b1 = (Button) findViewById(R.id.Button01); |
057 |
Button b2 = (Button) findViewById(R.id.Button02); |
058 |
Button b3 = (Button) findViewById(R.id.Button03); |
061 |
View.OnClickListener b_ocl= new View.OnClickListener() { |
064 |
public void onClick(View v) { |
068 |
showDialog(Edward_Movie_Dialog); |
072 |
dismissDialog(Edward_Movie_Dialog); |
076 |
removeDialog(Edward_Movie_Dialog); |
083 |
b1.setOnClickListener(b_ocl); |
084 |
b2.setOnClickListener(b_ocl); |
085 |
b3.setOnClickListener(b_ocl); |
091 |
public Dialog onCreateDialog( int id) { |
092 |
Toast.makeText( this , "onCreateDialog方法被调用" , Toast.LENGTH_LONG).show(); |
094 |
return new AlertDialog.Builder( this ) |
095 |
.setIcon(android.R.drawable.btn_star).setTitle( "喜好调查" ) |
096 |
.setMessage( "你喜欢看爱德华.诺顿Edward Norton的电影吗?" ) |
097 |
.setNegativeButton( "不喜欢" , ocl).setNeutralButton( "一般般" , ocl) |
098 |
.setPositiveButton( "很喜欢" , ocl).create(); |
103 |
public void onPrepareDialog( int id, Dialog dialog){ |
104 |
Toast.makeText( this , "onPrepareDialog方法被调用" , Toast.LENGTH_LONG).show(); |
105 |
super .onPrepareDialog(id, dialog); |
4、运行结果如下:
有兴趣的同学可以考虑一下如何改进关闭和移除对话框按钮。
三、自定义对话框 Custom Dialog
(待续)
转自:http://android.yaohuiji.com/archives/655
- 探索Popupwindow-对话框风格的窗口(
- Android——用XML的selector实现按钮多态
- Android学习03
- Android(安卓)自定义实现switch开关按钮
- Android中使用BottomNavigationBar实现仿微信底部按钮
- Android使用Intent返回上一个Activity时StackOverflowError
- Android(安卓)ADT 10.0.0下载与安装
- 如何把应用跑在android上
- android 从顶层到底层 学习笔记--- framework架构
随机推荐
-
求SQL语句,有没有周末上班的高手,给帮个忙
-
谁能给我讲讲用C#在windows mobile上开发
-
Sql Server系列:Insert语句
-
CentOS系统操作mysql的常用命令
-
xp使用mysql出现的问题
-
Oracle的临时表空间
-
数据库不支持中文解决方案(mysql)
-
sql2000 第一次执行慢
-
SQL 在线 UML 设计器 — 使用详解
-
我应该如何选择一列与另一列相关的语句?