当我点击按钮BUTTON
时,我正在显示一个ProgressDialog弹出窗口,其中显示了圆形循环进度表的内容。现在应该运行N秒,之后,有一些代码只在临时创建的ProgressDialog实例消失后才会执行(N秒启动时)。代码在TextView中显示一些文本,每次点击BUTTON
时文本都会更改,并且图像也应该更改。但只有在ProgressDialog结束后才能看到更改
但问题是,只要我点击BUTTON
,文本就会改变,并且ProgressDialog循环对话框会播放N秒,这不应该发生 - 文本应该只在ProgressDialog之后改变完成盘旋N秒,应用程序View
重新聚焦,然后文本被更改。
以下是代码:
BUTTON.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
imageView.setVisibility(ImageView.INVISIBLE);
//Choosing MESXs type to show =================================
if(mTSFlag==1)
mTS=holA;
else
mTS=holB;
// ProgressDialog =================================
final ProgressDialog progressRing=ProgressDialog.show(MainActivity.this, "Please wait..", "This takes time..", true);
progressRing.setCancelable(false);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try
{
Thread.sleep(3000);
}catch(Exception e){}
progressRing.dismiss();
}
}).start();
//Image choosing logic =================================
imageView.setVisibility(ImageView.VISIBLE);
if(pTOGGLE)
imageView.setImageResource(R.drawable.IMG_XXT);
else
imageView.setImageResource(R.drawable.IMG_XYT);
wIHM_result.setText(mTS);
}
});
问题是,好像Image Logic
部分(标题注释标记)在代码的ProgressDialgo
部分之前运行。显示适当的图像,文本被更改,我可以在昏暗的应用程序屏幕中看到所有这些,而ProgressDialog正在进行到N秒的结束。
我希望它在进度对话盘旋事物完成盘旋并且焦点回到应用程序视图后显示已更改的图像和文本(代码中的对象 - 分别为imageView和wIHM)。
答案 0 :(得分:0)
使用AsynTask类。 编写按钮单击其中并使用AsynTask中的进度对话框..
AsynTask有initBackground(),preExecute(),postExecute()等方法。
在preExecute中启动进度对话框...在initBackground中写入逻辑,并使用ProgressDialog.dismiss()结束逻辑最后一行;并在postExecute();
中编写textchange代码和imagechange逻辑BUTTON.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new ABC.Execute();
}
});
public class ABC extends AsyncTask<Void,Void,Void>
{
ProgressDialog downloadProgressDialog =new ProgressDialog(Accounts.this);
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
//Set your text and image object here....
}
@Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
downloadProgressDialog.setMessage("Message");
downloadProgressDialog.setCancelable(false);
downloadProgressDialog.show();
}
@Override
protected Void doInBackground(Void... arg0)
{
//Write your code here......
downloadProgressDialog.dismiss();
}
}
答案 1 :(得分:0)
你最好使用AsyncTask来做。如果您想根据代码修复问题,您应该这样做:
BUTTON.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
imageView.setVisibility(ImageView.INVISIBLE);
//Choosing MESXs type to show =================================
if(mTSFlag==1)
mTS=holA;
else
mTS=holB;
// ProgressDialog =================================
final ProgressDialog progressRing=ProgressDialog.show(MainActivity.this, "Please wait..", "This takes time..", true);
progressRing.setCancelable(false);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try{
Thread.sleep(3000);
//Don't run your UI method in non-Ui thread.
//move progressRing.dismiss() to runOnUiThread();
yourActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progressRing.dismiss();
//Image choosing logic =================================
imageView.setVisibility(ImageView.VISIBLE);
if(pTOGGLE)
imageView.setImageResource(R.drawable.IMG_XXT);
else
imageView.setImageResource(R.drawable.IMG_XYT);
wIHM_result.setText(mTS);
}
}catch(Exception e){
}
}
}).start();
}
});
答案 2 :(得分:0)
drawRect
progressRing.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
// Image choosing logic codes should be here for processing after dialog showed
}
});
设置在显示对话框时调用的侦听器