按钮上点击进度条 [英] Progress bar on a button click
问题描述
在我的应用程序,在点击按钮,应用程序做了一些处理,通过网络发送的一些数据,然后停止。由于需要一定的时间,我试图把一个进度条。在code进度条是的onclick监听器按钮的开始。进度条$ C $℃后,在网络处理和发送数据发生。
但是,进度条是不可见的。我需要一定显示在一个单独的线程中的进度条?
这是我用来显示进度条
最后ProgressDialog PD =新ProgressDialog(本);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage(请稍候...);
pd.setCancelable(真);
pd.setIndeterminate(真);
pd.show();
使用这是用来在后台做的过程中的AsyncTask类,你也可以显示你的进度有
下面是code,为的AsyncTask简单的代码片段
类BackgroundProcess扩展的AsyncTask<太虚,太虚,太虚> {
私人ProgressDialog进展情况;
公共doInBackground(无效... ARG){
publishProgress();
//做你的处理喜欢这里发送数据或下载等。
}
@覆盖
保护无效onProgressUpdate(虚空......值){
super.onProgressUpdate(值);
进度= ProgressDialog.show(YourActivity.this,,等待...);
}
@覆盖
保护无效onPostExecute(虚空结果){
super.onPostExecute(结果);
如果(进步!= NULL)
progress.dismiss();
进度= NULL;
}
}
现在初始化并在按钮的onclick监听器执行它像这样
新BackgroundProcess()执行();
现在progressdialog将发布和显示在屏幕上,当过程从onPostExecute(完成当时)刚刚解雇进度对话框
In my app, at the click of a button, the app does some processing, sends some data over the network and then stops. As it takes some time, I tried putting in a progress bar. The code for the progress bar is at the beginning of the onclick listener for the button. After the progress bar code, the processing and sending data over the network takes place. But the progress bar is not visible at all. Do I need to necessarily show the progress bar in a separate thread?
This is what i have used to show the progress bar
final ProgressDialog pd=new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage("Please wait..");
pd.setCancelable(true);
pd.setIndeterminate(true);
pd.show();
use the AsyncTask class which was used to do process in background and you can also showing up your progressbar there
Here is the simple snippet of code for AsyncTask
class BackgroundProcess extends AsyncTask<Void,Void,Void>{
private ProgressDialog progress;
public doInBackground(Void...arg){
publishProgress();
// do your processing here like sending data or downloading etc.
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
progress = ProgressDialog.show(YourActivity.this, "", "Wait...");
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(progress!=null)
progress.dismiss();
progress = null;
}
}
now initialize and execute it in button onclick listener like this way
new BackgroundProcess().execute();
now progressdialog will be publish and appear on the screen and when process was completed then from the onPostExecute() just dismiss the progress dialog
这篇关于按钮上点击进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!