免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5221 | 回复: 2
打印 上一主题 下一主题

Android消息提示框和对话框 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-11-09 12:23 |只看该作者 |倒序浏览
转自:Benjamin Wang's Blog


在某些情况下需要向用户弹出提示消息,如显示错误信息,收到短消息等,Android提供两种弹出消息的方式,消息提示框toasts和对话框alerts。
Toast是一种短暂的消息提示,显示一段时间后不需要用户交互会自动消失,所以用来显示一些建议性的不太重要的消息,如提示用户后台一个任务完成了。
使用Toast来弹出提示消息也很简单,调用Toast类的静态方法makeText():
  1. public static Toast makeText (Context context, CharSequence text, int duration)
复制代码
context: 调用的上下文,通常为Application或Activity对象
text: 显示的消息
duration: 显示的时间长短,为 Toast.LENGTH_LONG或Toast.LENGTH_SHORT

如可以这样调用:Toast.makeText(this, "Deleted Successfully!", Toast.LENGTH_LONG).show(); 效果如下:

AlertDialog类似于传统的模式对话框,需要与用户交互后才会关闭。
最简单的创建AlertDialog对话框的方法是使用AlertDialog的嵌套类Builder,它有下面几个主要的方法:

setMessage(): 设置显示的消息内容
setTitle() 和setIcon(): 设置对话框的标题栏的文字和图标
setPositiveButton(), setNeutralButton()和setNegativeButton(): 设置对话框的按钮,包括按钮显示的文字,按钮点击的事件
setView(): 设置对话框显示一个自定义的视图

自定义视图addemployee.xml代码如下, 需要注意的是布局文件的名称只能包含“a-z0-9_.”,不然就会报这样的错误:“Invalid file name: must contain only [a-z0-9_.]”
  1. <?xml version="1.0" encoding="utf-8"?>
  2. 02        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. 03            android:layout_width="fill_parent" android:layout_height="fill_parent"
  4. 04            android:orientation="vertical">
  5. 05            <LinearLayout android:layout_width="fill_parent"
  6. 06                android:layout_height="wrap_content" android:orientation="horizontal">
  7. 07                <TextView android:layout_width="wrap_content"
  8. 08                    android:layout_height="wrap_content" android:text="Name:" />
  9. 09                <EditText android:layout_width="fill_parent"
  10. 10                    android:layout_height="wrap_content" android:id="@+id/editName"></EditText>
  11. 11            </LinearLayout>
  12. 12            <LinearLayout android:layout_width="fill_parent"
  13. 13                android:layout_height="wrap_content" android:orientation="horizontal">
  14. 14                <TextView android:layout_width="wrap_content"
  15. 15                    android:layout_height="wrap_content" android:text="Age:"></TextView>
  16. 16                <EditText android:layout_width="fill_parent"
  17. 17                    android:layout_height="wrap_content" android:id="@+id/editAge" android:inputType="number"></EditText>
  18. 18            </LinearLayout>
  19. 19        </LinearLayout>
复制代码
生成对话框的代码如下所示:
  1.     LayoutInflater layoutInflater = LayoutInflater.from(this);
  2. 02        viewAddEmployee = layoutInflater.inflate(R.layout.addemployee, null);
  3. 03         
  4. 04        new AlertDialog.Builder(this).setTitle("Add Employee").setView(
  5. 05               viewAddEmployee).setPositiveButton("OK",
  6. 06               new DialogInterface.OnClickListener() {
  7. 07                   @Override
  8. 08                   public void onClick(DialogInterface dialog, int which) {
  9. 09                       insertEmployee();
  10. 10                   }
  11. 11               }).setNegativeButton("Cancel",
  12. 12               new DialogInterface.OnClickListener() {
  13. 13                   @Override
  14. 14                   public void onClick(DialogInterface dialog, int which) {
  15. 15                         
  16. 16                   }
  17. 17               }).show();
复制代码
这里先加载了一个自定义的视图, 并通过setView()设置对话框显示这个自定义视图, 并添加了两个按钮和相应的点击事件, 运行效果如下:

希望本文对您有所帮助。



参考书籍:Beginning Android 2 和Android官方文档

论坛徽章:
0
2 [报告]
发表于 2010-12-03 19:32 |只看该作者
记个书上都有啊

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
3 [报告]
发表于 2010-12-06 09:44 |只看该作者
很好。学习。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP