简单案例:
说明:如图所示,点击两个Action,两个ProgressBar开始加载,进度条滚动完毕后切换为下载完成。
代码:
xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProgressBarActivity"
android:orientation="vertical">
<ProgressBar
android:max="100"
android:progress="0"
android:secondaryProgress="80"
android:id="@+id/progressBar_PB"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/result_TV"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textSize="20sp"
android:text="准备下载。。。"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/progress_btn"
android:text="Action"/>
<ProgressBar
android:max="100"
android:id="@+id/progressBar2_PB"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/result2_TV"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textSize="20sp"
android:text="准备下载。。。"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/progress2_btn"
android:text="Action"/>
</LinearLayout>
java文件
package com.example.fpl.food;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.TextView;
public class ProgressBarActivity extends AppCompatActivity implements View.OnClickListener {
private ProgressBar progressBar;
private Button button;
private ProgressBar progressBar2;
private Button button2;
private TextView textView2;
private TextView resulttextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress_bar);
progressBar=findViewById(R.id.progressBar_PB);
button=findViewById(R.id.progress_btn);
progressBar2=findViewById(R.id.progressBar2_PB);
button2=findViewById(R.id.progress2_btn);
textView2=findViewById(R.id.result2_TV);
resulttextView=findViewById(R.id.result_TV);
button.setOnClickListener(this);
button2.setOnClickListener(this);
}
//使用AsyncTask类,就是创建一个类并继承它,后面带有三个参数
class progressBarTask extends AsyncTask<Integer,Integer,Integer>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
//
@Override
//后台主线程的方法,参数来自上面三个的第一个
protected Integer doInBackground(Integer... index) {
int increase=10;
//while循环让进度条滚动的原理加载带100,就跳出结束
while (increase<=100) {
//使用publishprogress()方法,触发下面的更新操作onProgressUpdate并且传出两个值
publishProgress(increase ,index[0]);
//进度条每次增加10
increase+=10;
//进度条1000毫秒更新一次
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//返回值会被onPostExecute()接收
return index[0];
}
//接受前面publishprogress传过来的两个值转入数组
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//通过前面传过来的第二个值判断是哪个进度条
switch (values[1]){
case 1:
//第一个进度条加改变进度条
progressBar.setProgress(values[0]);
break;
case 2:
progressBar2.setProgress(values[0]);
break;
}
}
//后台线程处理完后
@Override
protected void onPostExecute(Integer s) {
super.onPostExecute(s);
switch (s){
case 1:
//进度条滚动完毕后按钮恢复
button.setEnabled(true);
resulttextView.setText("下载完成");
break;
case 2:
button2.setEnabled(true);
textView2.setText("下载完成");
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.progress_btn:
//点击第一个按钮后,按钮关闭
button.setEnabled(false);
new progressBarTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,1);
break;
case R.id.progress2_btn:
button2.setEnabled(false);
new progressBarTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,2);
}
}
}
985

被折叠的 条评论
为什么被折叠?



