机器人 - 进度延迟加载时,按一下按钮 [英] android - Progressbar Late Loading when button click
问题描述
在我的应用程序,当我点击按钮,进度条会迟后开始。我想很快开始启动。请任何人可以帮助我。 bshowgifts听者的onCreate()。
bShowGifts.setOnClickListener(新OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
// vsChange
progressDlg = ProgressDialog.show(Ehome.this,,,真);
progressDlg.setContentView(R.layout.custom_dialog);
//性别和年龄,必须选择
如果(ElGiftoAppData.getAppData()。arSelectedGender.size()== 0)
{
progressDlg.dismiss();
字符串strMsg =请选择性别;
DisplaySelectionMessage(strMsg);
返回;
}
如果(ElGiftoAppData.getAppData()。arSelectedAge.size()== 0)
{
progressDlg.dismiss();
字符串strMsg =请选择一个时代;
DisplaySelectionMessage(strMsg);
返回;
}
如果(nMatchingGifts == 0)
{
progressDlg.dismiss();
tvCount =(TextView中)findViewById(R.id.textMatchinGifts);
字符串strContent =#匹配礼品:0;
tvCount.setText(strContent);
字符串strMsg =没有找到礼物请更改搜索条件!
DisplaySelectionMessage(strMsg);
返回;
}
尝试
{
//线程获取的负面属性值
螺纹tDisplayCategories =新的Thread()
{
公共无效的run()
{
的System.out.println(Showgifts:主题:运行);
handlerDisplayGifts.post(call_ShowGiftsCategoryPage);
}
};
tDisplayCategories.start();
}
赶上(例外EXP)
{
progressDlg.dismiss();
}
返回;
}
});
公共无效DisplaySelectionMessage(字符串strMsg)
{
//显示警告对话框
AlertDialog alertDialog =新AlertDialog.Builder(Ehome.this).create();
alertDialog.setTitle(Elgifto警报);
alertDialog.setMessage(strMsg);
alertDialog.setButton(OK,新DialogInterface.OnClickListener()
{
公共无效的onClick(DialogInterface对话框,INT ID)
{
dialog.cancel();
}
});
alertDialog.show();
返回;
}
更新code
bShowGifts.setOnClickListener(新OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
新showgiftsbtn()执行()。
}
});
公共类showgiftsbtn扩展的AsyncTask<虚空,虚空,虚空>
{
私人最终ProgressDialog对话框=新ProgressDialog(Ehome.this);
在preExecute保护无效()
{
this.dialog.setMessage(请稍候...);
this.dialog.show();
//把你的code $它与processDialog p $ PLOAD
如果(ElGiftoAppData.getAppData()。arSelectedGender.size()== 0)
{
dialog.dismiss();
字符串strMsg =请选择性别;
DisplaySelectionMessage(strMsg);
返回;
}
如果(ElGiftoAppData.getAppData()。arSelectedAge.size()== 0)
{
dialog.dismiss();
字符串strMsg =请选择一个时代;
DisplaySelectionMessage(strMsg);
返回;
}
如果(nMatchingGifts == 0)
{
dialog.dismiss();
tvCount =(TextView中)findViewById(R.id.textMatchinGifts);
字符串strContent =#匹配礼品:0;
tvCount.setText(strContent);
字符串strMsg =没有找到礼物请更改搜索条件!
DisplaySelectionMessage(strMsg);
返回;
}
尝试
{
//线程获取的负面属性值
螺纹tDisplayCategories =新的Thread()
{
公共无效的run()
{
的System.out.println(Showgifts:主题:运行);
handlerDisplayGifts.post(call_ShowGiftsCategoryPage);
dialog.dismiss();
}
};
tDisplayCategories.start();
}
赶上(例外EXP)
{
dialog.dismiss();
}
}
@覆盖
保护无效doInBackground(空...为arg0)
{
//把你这里code
返回null;
}
保护无效onPostExecute(最终虚空未使用)
{
}
}
试试这个:: AsyncTask的能够正确且易于使用的用户界面线程。该类允许无需操作线程和/或处理程序进行后台操作并发布在UI线程上的结果。
这是异步任务是由在后台线程运行的计算,其结果发表在UI线程上定义。异步任务由3泛型类型,名为PARAMS,进展和成效,以及4个步骤定义,呼吁preExecute,doInBackground,onProgressUpdate和onPostExecute
私有类XYZ扩展的AsyncTask<虚空,虚空,虚空> {
私人最终ProgressDialog对话框=新ProgressDialog(tranning.this);
在preExecute保护无效(){
this.dialog.setMessage(请稍候...);
this.dialog.show();
//把你的code $它与processDialog p $ PLOAD
@覆盖
保护无效doInBackground(虚空......为arg0){
//把你这里code
如果(ElGiftoAppData.getAppData()。arSelectedGender.size()== 0)
{
字符串strMsg =请选择性别;
DisplaySelectionMessage(strMsg);
返回;
}
如果(ElGiftoAppData.getAppData()。arSelectedAge.size()== 0)
{
progressDlg.dismiss();
字符串strMsg =请选择一个时代;
DisplaySelectionMessage(strMsg);
返回;
}
如果(nMatchingGifts == 0)
{
tvCount =(TextView中)findViewById(R.id.textMatchinGifts);
字符串strContent =#匹配礼品:0;
tvCount.setText(strContent);
字符串strMsg =没有找到礼物请更改搜索条件!
DisplaySelectionMessage(strMsg);
返回;
}
尝试
{
//线程获取的负面属性值
螺纹tDisplayCategories =新的Thread()
{
公共无效的run()
{
的System.out.println(Showgifts:主题:运行);
handlerDisplayGifts.post(call_ShowGiftsCategoryPage);
}
};
tDisplayCategories.start();
}
赶上(例外EXP)
{
}
}
返回null;
}
保护无效onPostExecute(最终虚空未使用){
如果(this.dialog.isShowing()){
this.dialog.dismiss();
}
}
}
和使用本在你的按钮点击事件::
新的xyz()执行()。
In my app when i click button the progress bar will be started lately. I want to start start quickly. please can anybody help me. bshowgifts listener in 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;
}
Updated code
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)
{
}
}
try this:: AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and 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();
}
}
}
and use this in your button click event ::
new xyz().execute();
这篇关于机器人 - 进度延迟加载时,按一下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!