我希望在从服务器中检索数据时,在onpreexecute和onpost执行时显示progreebar而不是progressdialog。
这是我的异步方法
@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);
}
这个进度条在progress_bar布局中,这是一个单独的布局,这个onPreecute方法在登录类中
答案 0 :(得分:0)
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();
}
}
答案 1 :(得分:0)
普通模板就是这样的
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);
}
}
答案 2 :(得分:0)
使用的代码是这样的..我创建了一个单独的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>
和onCreate或onCreateView上的java代码初始化了Dialog
dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.prograss_bar_dialog);
并且还编写了两个函数,如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()
希望这会有所帮助
答案 3 :(得分:0)
首先将ProgressBar视图添加到布局
<ProgressBar
android:id="@+id/progress"
android:layout_width="24dp"
android:layout_height="24dp"
android:visibility="invisible" />
在MyAwesomeAsynkTaskClass中初始化进度条,如
private ProgressBar mProgressBar;
通过以下步骤修改onPreExecute()和onPostExecute():
protected void onPreExecute() {
super.onPreExecute();
mProgressBar.setVisibility(View.VISIBLE);
}
和
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
mProgressBar.setVisibility(View.GONE);
}