如何在android的AsynkTask中显示Progressbar
我想在从服务器检索数据时在onpreexecute和onpost execute中显示progreebar而不是progressdialog 这是我的异步方法如何在android的AsynkTask中显示Progressbar,android,android-layout,Android,Android Layout,我想在从服务器检索数据时在onpreexecute和onpost execute中显示progreebar而不是progressdialog 这是我的异步方法 @Override protected void onPreExecute() { super.onPreExecute(); m_ProgressBar = new ProgressBar(getActivity(), null, R.layout.progress_bar); m_ProgressBar = (
@Override
protected void onPreExecute() {
super.onPreExecute();
m_ProgressBar = new ProgressBar(getActivity(), null, R.layout.progress_bar);
m_ProgressBar = (ProgressBar) m_Main.findViewById(R.id.progressbar);
m_ProgressBar.setVisibility(View.VISIBLE);
}
当此progressbar正在进行时\u bar布局是独立的布局,并且此onPreecute方法位于登录类中时,正常模板如下所示
Below code snippet uses will help you
private class SaveImageTask extends AsyncTask<Bitmap,Void,Void>{
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Saving image....");
progressDialog.show();
}
@Override
protected Void doInBackground(Bitmap... params) {
Bitmap bitmap = params[0];
FileOutputStream out = null;
String timeStamp = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date());
createDirectory();
try {
out = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/"+Environment.DIRECTORY_PICTURES+"/QuoteCards/IMG_"+timeStamp+".png"));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
frameLayout.destroyDrawingCache();
Toast.makeText(MainActivity.this,"Image has been saved in SD Card",Toast.LENGTH_SHORT).show();
scan();
}
}
private class MyAsyncTask extends AsyncTask<String, Void, Bitmap> {
protected void onPreExecute() {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
protected Bitmap doInBackground(String... strings) {
Bitmap = downloadImageFromUrl(strings[0]);
return someBitmap;
}
protected void onProgressUpdate(Progress... values) {
progressBar.setProgress(values[0]);
}
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
// Hide the progress bar
progressBar.setVisibility(ProgressBar.INVISIBLE);
}
}
使用的代码如下所示。。我创建了一个单独的diloag.xml,并在其中包含了进度条
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="65dp"
android:background="@android:color/background_dark"
android:orientation="vertical">
<TextView
android:id="@+id/progress_text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_above="@+id/progress_bar"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:background="@android:color/transparent"
android:gravity="center_vertical"
android:text=""
android:textColor="@android:color/white"
android:textSize="16sp"
android:visibility="gone" />
<ProgressBar
android:id="@+id/progress_bar"
style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:background="@color/cardview_dark_background"
android:maxHeight="20dp"
android:minHeight="20dp" />
</RelativeLayout>
并编写了showPB和hidePB两个函数
public void showPB(String message) {
progressBar = (ProgressBar) dialog.findViewById(R.id.progress_bar);
TextView progressText = (TextView) dialog.findViewById(R.id.progress_text);
progressText.setText("" + message);
progressText.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
public void hidePB() {
//if (dialog != null) {
dialog.dismiss();
//}
}
需要时随时打电话,也一定要打hidePB
希望这有助于首先将ProgressBar视图添加到布局中
<ProgressBar
android:id="@+id/progress"
android:layout_width="24dp"
android:layout_height="24dp"
android:visibility="invisible" />
并通过以下步骤修改onPreExecute和onPostExecute:
protected void onPreExecute() {
super.onPreExecute();
mProgressBar.setVisibility(View.VISIBLE);
}
及
在谷歌搜索你会找到很多解决方案他问ProgressBar不是progressdialog是的正确我不是问progressdialog我想要ProgressBar而不是progressdialog@abhishek-在这种情况下,您可以在布局xml中添加progressbar,并将其可见性设置为GONE。然后,在onPreExecute中,您必须使其可见,在onPostExecute中,您必须使其可见,因为它再次消失。谢谢…………您给了我正确的解决方案,那么我应该得到一次向上投票,对吗D
private ProgressBar mProgressBar;
protected void onPreExecute() {
super.onPreExecute();
mProgressBar.setVisibility(View.VISIBLE);
}
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
mProgressBar.setVisibility(View.GONE);
}