Android AlertDialog 动态更新弹出框中的内容

AlertDialog弹出框中内容自动更新,效果图:


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();
			}
		}
    };
}




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值