安卓ProgressDialog.show(),无法显示等待框

先给大家把我的解决代码贴出来:

package com.park;



import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelUuid;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.ConsoleMessage.MessageLevel;
import android.widget.ImageView;
import android.widget.TextView;
import android.bluetooth.*;
import android.widget.Toast;


/**
 * Created by lenovo on 2015/5/25.
 */
public class CommunityParking extends Activity implements View.OnClickListener {


private ProgressDialog progressDialog, progressDialog2, progressDialog3;
private int parkNum = 50;
private ImageView back, around_parking, zero, ten, oneHour, threeHour;
private Intent intent;
private TextView num;
private Handler handler;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.community_parking);

num = (TextView) findViewById(R.id.num);
int a = 10;
back = (ImageView) findViewById(R.id.back);
around_parking = (ImageView) findViewById(R.id.around_parking);
back.setOnClickListener(this);
around_parking.setOnClickListener(this);
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {


@Override
public void run() {


// TODO Auto-generated method stub
runOnUiThread(new Runnable() {


public void run() {
parkNum++;
num.setText("" + parkNum);
progressDialog.dismiss();
}
});
}
};
progressDialog = ProgressDialog.show(this, "", "数据加载中,请稍后...");
timer.schedule(timerTask, 4000, 4000);
zero = (ImageView) findViewById(R.id.zero);
ten = (ImageView) findViewById(R.id.ten);
oneHour = (ImageView) findViewById(R.id.oneHour);
oneHour.setOnClickListener(new test());
threeHour = (ImageView) findViewById(R.id.threeHour);
threeHour.setOnClickListener(new test2());
}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.back:
intent = new Intent(this, MainActivity.class);
startActivity(intent);
break;
case R.id.around_parking:
intent = new Intent();
intent.setAction("android.intent.action.AROUPARK");
startActivity(intent);
break;
}
}


class test implements OnClickListener {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progressDialog2 = ProgressDialog.show(CommunityParking.this, "",
"车位估算中,请稍后...");
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
progressDialog2.dismiss();
ten.setVisibility(View.VISIBLE);
}
}, 1000);
}
}


class test2 implements OnClickListener {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progressDialog3 = ProgressDialog.show(CommunityParking.this, "",
"车位估算中,请稍后...");
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
progressDialog3.dismiss();
zero.setVisibility(View.VISIBLE);
}
}, 1000);
}
}

}

是这样的,我在oncreate()里面执行的,progressdialog.show();可以正常的显示进度框,但是当我给xml页面中的一个imageview设置点击事件之后,调用progressdialgo.show()无法显示进度框。因为一开始我是这样定义的点击时间,我让CommunityParking  implements Onclicklistner,然后同意去处理点击监控事件,但是我发现这种方法无法显示等待对话框,然后我用通过new test(),新建出点击事件的监听类,然后就可以显示等待对话框了。现在还不知道为什么会这样,但是问题已经解决了。

还有一点,一开始我用的thread.sleep().让线程暂停两秒,但是这样的话总是出错,而且无法显示等待提示框,后来我搜的stackoverflow,上面说用handler去处理程序等待问题,最后问题解决。具体大家可以参考我的代码,有什么问题可以留言。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。