子线程控制ProgressDialog的可见性

这个代码让我有点迷惑:

package com.example;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Looper_07 extends Activity implements OnClickListener {
private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
private ProgressDialog progressDialog = null;
public TextView tv;
private Button btn, btn2;
Thread th1;

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);

btn = new Button(this);
btn.setId(101);
btn.setBackgroundResource(R.drawable.icon);
btn.setText("test looper");
btn.setOnClickListener(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100, 50);
param.topMargin = 10;
layout.addView(btn, param);

btn2 = new Button(this);
btn2.setId(102);
btn2.setBackgroundResource(R.drawable.icon);
btn2.setText("exit");
btn2.setOnClickListener(this);
layout.addView(btn2, param);

tv = new TextView(this);
tv.setTextColor(Color.WHITE);
tv.setText("");
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(FP, WC);
param2.topMargin = 10;
layout.addView(tv, param2);
setContentView(layout);
// ------------------------
}

public void onClick(View v) {
switch (v.getId()) {
case 101:
progressDialog = ProgressDialog.show(this,
"please wait…","Loading",true);
th1 = new myThread();
th1.start();
setTitle("mainThread....");
break;
case 102:
finish();
break;
}
}
class myThread extends Thread {
@Override
public void run() {
try{
sleep(3000); //故意延遲
}
catch(Exception e)
{
e.printStackTrace();
}
// 为什么这里可以控制Dialog的可见性????????????
//子线程来执行
progressDialog.dismiss();
}
}
}
/*
上述程式又可寫為:
public class ac01 extends Activity
implements OnClickListener, Runnable{
//………………(省略)
public void onClick(View v) {
switch(v.getId()){
case 101:
progressDialog = ProgressDialog.show(this,
"please wait…","Loading",true);
th1 = new Thread(this);
th1.start();
setTitle("mainThread....");
break;
case 102:
finish();
break;
}
}
public void run() {
try{
Thread.sleep(6000); //故意延遲
}
catch(Exception e)
{
e.printStackTrace();
}
progressDialog.dismiss();
}
}
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值