android-进度条延迟加载,当点击按钮时
问题描述:
在我的应用程序中,当我点击按钮时,进度条将最近启动。我想快速开始。请任何人都可以帮助我。 onCreate()中的bshowgifts监听器。android-进度条延迟加载,当点击按钮时
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)
{
}
}
答
尝试这种:: 的AsyncTask能够正确且容易使用的UI线程。该类允许执行后台操作并在UI线程上发布结果,而无需操纵线程和/或处理程序。
异步任务由在后台线程上运行并且其结果在UI线程上发布的计算定义。异步任务由3种泛型类型,称为参数,可以进展和结果,以及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();
答
使用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);
}
};
把上面的代码上面的类代码并称之为其中显示进度条( );像这样:
mHandler.post(mUpdateResults);
单击事件表示单击侦听器。这是对的吗?我把这样的bShowGifts.setOnClickListener(新OnClickListener() \t \t { \t \t \t @覆盖 \t \t \t公共无效的onClick(视图v) \t \t \t {\t \t \t \t \t新showgiftsbtn()。执行(); \t} \t \t}); – naresh
这是行不通的 – naresh
public void onClick(View v) { new xyz()。execute(); –