在我的应用程序中,当我单击按钮时,最近将启动进度条。我想快点开始。请任何人帮助我。在onCreate()中bshowgifts监听器。
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)
{
}
}
答案 0 :(得分:4)
试试这个:: AsyncTask可以正确,方便地使用UI线程。该类允许执行后台操作并在UI线程上发布结果,而无需操纵线程和/或处理程序。
异步任务由在后台线程上运行的计算定义,其结果在UI线程上发布。异步任务由3种泛型类型定义,称为Params,Progress和Result,以及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();
}
}
}
并在按钮点击事件::
中使用此功能 new xyz().execute();
答案 1 :(得分:0)
使用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 );