设为首页
优惠IDC
收藏本站
六狼博客
六狼论坛
开启辅助访问
切换到窄版
用户名
Email
自动登录
找回密码
密码
登录
立即注册
只需一步,快速开始
只需一步,快速开始
快捷导航
门户
首页
BBS
云计算
大数据
手机
移动开发android,ios,windows phone,windows mobile
编程
编程技术java,php,python,delphi,ruby,c,c++
前端
WEB前端htmlcss,javascript,jquery,html5
数据库
数据库开发Access,mysql,oracle,sql server,MongoDB
系统
操作系统windows,linux,unix,os,RedHat,tomcat
架构
项目管理
软件设计,架构设计,面向对象,设计模式,项目管理
企业
服务
运维实战
神马
搜索
搜索
热搜:
php
java
python
ruby
hadoop
sphinx
solr
ios
android
windows
centos
本版
帖子
用户
六狼论坛
»
首页
›
操作系统
›
os
›
动态更新 AlertDialog 的问题
返回列表
查看:
80
|
回复:
0
动态更新 AlertDialog 的问题
[复制链接]
shaobin0604
shaobin0604
当前离线
积分
123
窥视卡
雷达卡
升级
48.67%
当前用户组为
秀才
当前积分为
123
, 升到下一级还需要 77 点。
31
主题
31
主题
31
主题
秀才
秀才, 积分 123, 距离下一级还需 77 积分
秀才, 积分 123, 距离下一级还需 77 积分
积分
123
发消息
楼主
|
发表于 2013-1-15 02:45:46
|
显示全部楼层
|
阅读模式
原文发布于:
http://aquarium.yo2.cn/articles/change-the-text-of-alertdialog-in-onpreparedialog.html
在 Android 开发中时常会遇到这样的问题:在删除列表中记录项时弹出确认对话框,对话框的提示消息取决于用户选中的项。例如:Are you sure you want to delete XXX。XXX由用户选中的记录项确定。
通常可以在 Activity 的 onCreateDialog 创建托管对话框(免去对话框状态管理的问题)。在调用 showDialog 的时候,如果该对话框还没有创建,则先调用 onCreateDialog 创建该对话框。随后 onPreparedDialog 被调用。因此,可以在 onPreparedDialog 中更新对话框的内容。
这里有一个诡异的问题:如果 AlertDialog 是在 Activity 的 onCreateDialog 中通过 AlertDialog.Builder 创建的,并且想在 onPreparedDialog 中改变其状态(比如:setTitle, setMessage),你必须先在 onCreateDialog 中调用 AlertDialog.Builder 的方法设置其初始值,否则在 onPreparedDialog 中的方法调用无效。下面通过代码说明此问题
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><Button android:id="@+id/btn_show_1" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="show dialog 1" /><Button android:id="@+id/btn_show_2" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="show dialog 2" /></LinearLayout>
在界面里放置了两个 Button ,分别用于显示 AlertDialog 。
Main.java
package cn.yo2.aquarium.alertdialogtest;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.os.Bundle;import android.text.format.DateFormat;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class Main extends Activity {private static final int TIME_ALERT_DIALOG_1 = 1;private static final int TIME_ALERT_DIALOG_2 = 2;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button button1 = (Button) findViewById(R.id.btn_show_1);Button button2 = (Button) findViewById(R.id.btn_show_2);button1.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubshowDialog(TIME_ALERT_DIALOG_1);}});button2.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubshowDialog(TIME_ALERT_DIALOG_2);}});}@Overrideprotected Dialog onCreateDialog(int id) {switch (id) {case TIME_ALERT_DIALOG_1:AlertDialog.Builder timeDialog1 = new AlertDialog.Builder(this);timeDialog1.setTitle("Alert 1: have not called setMessage in onCreateDialog");return timeDialog1.create();case TIME_ALERT_DIALOG_2:AlertDialog.Builder timeDialog2 = new AlertDialog.Builder(this);timeDialog2.setTitle("Alert 2: have called setMessage in onCreateDialog");timeDialog2.setMessage("");return timeDialog2.create();default:break;}return super.onCreateDialog(id);}@Overrideprotected void onPrepareDialog(int id, Dialog dialog) {switch (id) {case TIME_ALERT_DIALOG_1:AlertDialog timeDialog1 = (AlertDialog) dialog;timeDialog1.setMessage(DateFormat.format("h:mm:ss", System.currentTimeMillis()));break;case TIME_ALERT_DIALOG_2:AlertDialog timeDialog2 = (AlertDialog) dialog;timeDialog2.setMessage(DateFormat.format("h:mm:ss", System.currentTimeMillis()));break;default:break;}super.onPrepareDialog(id, dialog);}}
这两个 AlertDialog 的区别在于 AlertDialog1 在创建的时候没有设置 Message。结果是尽管 AlertDialog1 在 onPreparedDialog 中设置了 Message, 但是 AlertDialog1 根本不会显示 Message 部分。
参考
Android AlertDialog with dynamically changing text on every request
回复
使用道具
举报
提升卡
置顶卡
沉默卡
喧嚣卡
变色卡
千斤顶
显身卡
返回列表
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
本版积分规则
发表回复
回帖后跳转到最后一页
Copyright © 2008-2020
六狼论坛
(https://it.6wolf.com) 版权所有 All Rights Reserved.
Powered by
Discuz!
X3.4
京ICP备14020293号-2
本网站内容均收集于互联网,如有问题请联系
QQ:389897944
予以删除
快速回复
返回顶部
返回列表