在我的应用程序中,当我单击按钮时,进度条将在最近启动。我想快点开始。谁能帮帮我吗。在onCreate()中展示礼物侦听器。
bShowGifts.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//vsChange
progressDlg = ProgressDialog.show(Ehome.this, "", "", true);
progressDlg.setContentView(R.layout.custom_dialog);
//Gender and age must be selected
if(ElGiftoAppData.getAppData().arSelectedGender.size() == 0)
{
progressDlg.dismiss();
String strMsg = "Please select a gender";
DisplaySelectionMessage(strMsg);
return;
}
if(ElGiftoAppData.getAppData().arSelectedAge.size() == 0)
{
progressDlg.dismiss();
String strMsg = "Please select an age";
DisplaySelectionMessage(strMsg);
return;
}
if(nMatchingGifts == 0)
{
progressDlg.dismiss();
tvCount = (TextView) findViewById(R.id.textMatchinGifts);
String strContent = "# of Matching Gifts: 0";
tvCount.setText(strContent);
String strMsg = "No gifts found! Please change the search criteria.";
DisplaySelectionMessage(strMsg);
return;
}
try
{
//Thread for getting the negative attributes values
Thread tDisplayCategories = new Thread()
{
public void run()
{
System.out.println("Showgifts : Thread:run");
handlerDisplayGifts.post(call_ShowGiftsCategoryPage);
}
};
tDisplayCategories.start();
}
catch(Exception exp)
{
progressDlg.dismiss();
}
return;
}
});
public void DisplaySelectionMessage(String strMsg)
{
//display alert dialog
AlertDialog alertDialog = new AlertDialog.Builder(Ehome.this).create();
alertDialog.setTitle("Elgifto Alert");
alertDialog.setMessage(strMsg);
alertDialog.setButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
alertDialog.show();
return;
}更新代码
bShowGifts.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
new showgiftsbtn().execute();
}
});
public class showgiftsbtn extends AsyncTask<Void, Void, Void>
{
private final ProgressDialog dialog = new ProgressDialog(Ehome.this);
protected void onPreExecute()
{
this.dialog.setMessage("Please Wait...");
this.dialog.show();
// put your code which preload with processDialog
if(ElGiftoAppData.getAppData().arSelectedGender.size() == 0)
{
dialog.dismiss();
String strMsg = "Please select a gender";
DisplaySelectionMessage(strMsg);
return;
}
if(ElGiftoAppData.getAppData().arSelectedAge.size() == 0)
{
dialog.dismiss();
String strMsg = "Please select an age";
DisplaySelectionMessage(strMsg);
return;
}
if(nMatchingGifts == 0)
{
dialog.dismiss();
tvCount = (TextView) findViewById(R.id.textMatchinGifts);
String strContent = "# of Matching Gifts: 0";
tvCount.setText(strContent);
String strMsg = "No gifts found! Please change the search criteria.";
DisplaySelectionMessage(strMsg);
return;
}
try
{
//Thread for getting the negative attributes values
Thread tDisplayCategories = new Thread()
{
public void run()
{
System.out.println("Showgifts : Thread:run");
handlerDisplayGifts.post(call_ShowGiftsCategoryPage);
dialog.dismiss();
}
};
tDisplayCategories.start();
}
catch(Exception exp)
{
dialog.dismiss();
}
}
@Override
protected Void doInBackground(Void... arg0)
{
// put you code here
return null;
}
protected void onPostExecute(final Void unused)
{
}
}发布于 2011-09-08 07:21:53
尝试如下:: AsyncTask支持正确和容易地使用UI线程。这个类允许在UI线程上执行后台操作和发布结果,而不必操作线程和/或处理程序。
异步任务由运行在后台线程上并在UI线程上发布其结果的计算定义。异步任务由3种泛型类型定义,称为Params、进度和结果,以及4个步骤,称为onPreExecute、doInBackground、onProgressUpdate和onPostExecute。
private class xyz extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(tranning.this);
protected void onPreExecute() {
this.dialog.setMessage("Please Wait...");
this.dialog.show();
// put your code which preload with processDialog
@Override
protected Void doInBackground(Void... arg0) {
// put you code here
if(ElGiftoAppData.getAppData().arSelectedGender.size() == 0)
{
String strMsg = "Please select a gender";
DisplaySelectionMessage(strMsg);
return;
}
if(ElGiftoAppData.getAppData().arSelectedAge.size() == 0)
{
progressDlg.dismiss();
String strMsg = "Please select an age";
DisplaySelectionMessage(strMsg);
return;
}
if(nMatchingGifts == 0)
{
tvCount = (TextView) findViewById(R.id.textMatchinGifts);
String strContent = "# of Matching Gifts: 0";
tvCount.setText(strContent);
String strMsg = "No gifts found! Please change the search criteria.";
DisplaySelectionMessage(strMsg);
return;
}
try
{
//Thread for getting the negative attributes values
Thread tDisplayCategories = new Thread()
{
public void run()
{
System.out.println("Showgifts : Thread:run");
handlerDisplayGifts.post(call_ShowGiftsCategoryPage);
}
};
tDisplayCategories.start();
}
catch(Exception exp)
{
}
}
return null;
}
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}并在单击event ::按钮中使用此方法
new xyz().execute();发布于 2011-09-08 07:20:35
用Handler显示ProgressBar() ..。
private Handler mHandler = new Handler();
final Runnable mUpdateResults = new Runnable() {
public void run() {
progressDlg = ProgressDialog.show(Ehome.this, "", "", true);
progressDlg.setContentView(R.layout.custom_dialog);
}
};将上面的代码放在类代码之上,并在显示progressBar()的地方调用它;如下所示:
mHandler.post(mUpdateResults );https://stackoverflow.com/questions/7344488
复制相似问题