Android AlertDialog 动态更新弹出框中的内容
2014-09-26 18:30
246 查看
AlertDialog弹出框中内容自动更新,效果图:
Android 的AlertDialog中的Message一旦设置,在Dialog弹出后,显示过程中,不能改变其中的Msg值,其中如果你使用
它不会生效。
但是我们有时候又需要动态改变框中的值,比如AlertDialog中显示倒计时,这个时候无法直接使用AlertDialog中自带的函数对其进行改变,下面给出一种通过Handle和View对AlertDialog中的内容在显示过程中进行动态改变的方法:
1、 将Dialog中的文字框,用一个LinearLayout代替,LinearLayout中装有一个TextView
2、 不断发送Handle消息,对TextView中的值进行改变;
具体源代码如下
main.xml
Android 的AlertDialog中的Message一旦设置,在Dialog弹出后,显示过程中,不能改变其中的Msg值,其中如果你使用
mAlertDialog.setMessage(“New Value”);
它不会生效。
但是我们有时候又需要动态改变框中的值,比如AlertDialog中显示倒计时,这个时候无法直接使用AlertDialog中自带的函数对其进行改变,下面给出一种通过Handle和View对AlertDialog中的内容在显示过程中进行动态改变的方法:
1、 将Dialog中的文字框,用一个LinearLayout代替,LinearLayout中装有一个TextView
2、 不断发送Handle消息,对TextView中的值进行改变;
具体源代码如下
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" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|center" android:layout_marginTop="20dp" android:text="show dialog" /> </LinearLayout>dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_dialog" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FF0000" android:textSize="25dp" android:gravity="center" /> </LinearLayout>MainActivity.java
package com.example.trytry; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.format.DateFormat; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private AlertDialog mAlertDialog = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View view = View.inflate(getApplicationContext(), R.layout.dialog_layout, null); mAlertDialog = new AlertDialog.Builder(this) .setView(view) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub mAlertDialog.cancel(); } }).create(); mAlertDialog.setTitle("mAlertDialog"); Button button = (Button) findViewById(R.id.btn_show); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub mAlertDialog.show(); mHandler.sendEmptyMessage(0); } }); } private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub int what = msg.what; if (what == 0) { //update TextView tv = (TextView) mAlertDialog.findViewById(R.id.tv_dialog); tv.setText(DateFormat.format("yyyy-MM-dd hh:mm:ss", System .currentTimeMillis()).toString()); if(mAlertDialog.isShowing()){ mHandler.sendEmptyMessageDelayed(0,1000); } }else { mAlertDialog.cancel(); } } }; }
相关文章推荐
- Android 底部弹出窗popuwindows含有list动态控制显示内容高度
- android开发中 如何动态更新TextView的内容?
- Android alertDialog 动态添加edittext无法弹出键盘解决方案
- android开发中 如何动态更新TextView的内容?
- android开发中 如何动态更新TextView的内容?
- 【android】EditText监听,根据输入内容查询数据并动态更新ListView
- Android笔记-ListView总结(多选框ListViiew,动态加载,多线程更新ListView中的进度条)
- android alertdialog 弹出框
- 【Android应用实例之四】计时器之通过Service&BroadcastReceiver实现UI动态更新
- 如何不用数据库实现内容的动态更新
- Android service broadcast 动态 更新 UI 界面
- Android笔记-ListView总结(多选框ListViiew,动态加载,多线程更新ListView中的进度条)
- Android-UI详解:软键盘弹出后,动态改变UI布局(IM聊天窗口设计示例)
- Android中用两层AlertDialog来进行弹出选择框信息选择
- 用弹出确认框来确认删除内容(信息动态生成)
- Android: ListView动态更新数据
- Android笔记-ListView总结(多选框ListViiew,动态加载,多线程更新ListView中的进度条)
- android 如何动态更新当前显示的Activity 的local
- Android AlertDialog动态添加组件和padding靠边问题
- Android动态显示隐藏密码输入框的内容