首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android中ProgressDialog的简略示例

2012-06-28 
Android中ProgressDialog的简单示例Android中ProgressDialog的使用: http://skycode.iteye.com/blog/34696

Android中ProgressDialog的简单示例
Android中ProgressDialog的使用: http://skycode.iteye.com/blog/346960
Android中ProgressDialog的简单示例: http://lveyo.iteye.com/blog/585771
Android ProgressDialog的两种用法: http://www.pocketdigi.com/20110510/271.html

在android中,通常我们无法在单独的线程中更新UI,而要在主线程中,这也就是为什么我们要使用 Handler了,当handler收到消息中,它会把它放入到队列中等待执行,通常来说这会很快被执行。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textview1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="ShowProgressDialog" /></LinearLayout>



package com.progress.dialog;import android.app.Activity;import android.app.ProgressDialog;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;public class ProgressDialogActivity extends Activity {    /** Called when the activity is first created. */private Button button1;private TextView textview1;private ProgressDialog progressDialog;private Handler handler = new Handler(){    @Override    public void handleMessage(android.os.Message msg) {    try {    if (progressDialog != null) {progressDialog.dismiss();}    textview1.setText("关闭等待框.");    } catch (Exception e) {// TODO: handle exceptionLog.v("ProgressDialogActivity", e.getMessage());}        };    };        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                button1 = (Button) findViewById(R.id.button1);        textview1 = (TextView)findViewById(R.id.textview1);                button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// 创建并赋值给 progressDialogprogressDialog = ProgressDialog.show(ProgressDialogActivity.this, "我的标题", "[3秒后关闭]Load......", true, false);textview1.setText("显示等待框.");//创建线程并启动new Thread(new Runnable(){@Overridepublic void run() {try {Thread.sleep(3000);handler.sendEmptyMessage(0);} catch (Exception e) {e.printStackTrace();}}}).start();}});    }    }

热点排行
Bad Request.