您的位置:首页 > 移动开发 > Android开发

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

2014-09-26 18:30 246 查看
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();
}
}
};
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: