实现进度条首先就要知道[AsyncTask],异步任务类
这里推荐这个大佬写的文章,写的很不错
1.xml布局
声明一个显示,两个按钮,一个进度条
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt"
android:text="还没开始"
android:textSize="28sp"
android:layout_centerInParent="true"
/>
<!--专门处理进度条 -->
<ProgressBar
android:id="@+id/prog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:progress="0"
android:layout_below="@id/txt"
android:layout_marginTop="30dp"
/>
<Button
android:id="@+id/bnt1"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="@drawable/start"
android:layout_below="@id/prog"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"
android:text="开始"
/>
<Button
android:id="@+id/bnt2"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="@drawable/stop"
android:layout_below="@id/prog"
android:layout_marginTop="30dp"
android:layout_marginLeft="220dp"
android:text="暂停"
/>
</RelativeLayout>
样式
2.MainActivity
2.1首先要知道异步任务类的使用步骤
2.1.1定义一个AsyncTask子类
确定参数类型
// Params --> execute方法的参数类型,doInBackground方法的参数类型
// Progress(进度)-->(pog.setProgress();为int类型) 所有传Integer
// Result
//因为不需要返回值所以写void
class PogTask extends AsyncTask<Void,Integer,String>{
}
2.1.2实例化子类
//首先清楚进度条的运行顺序
//onPreExecute(text:加载中)-->doInBackground-->onProgressUpdate-->onPostExecute
// 取消 -->onCancelled
@Override
//执行线程任务的页面操作,
protected void onPreExecute() {
super.onPreExecute();
}
@Override
//不能处理UI操作,处理耗时操作(接受输入参数,返回线程执行结果)
protected String doInBackground(Void... voids) {
//模拟耗时操作
return null;
}
@Override
//在主线程中显示线程任务的执行进度,在doInBackground方法中调用publishProgress()方法会触发该方法
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
//接受线程任务的执行结果,将执行结果显示在页面上
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
2.1.3启动方法 -->取消
@Override
//取消(cancel)异步任务时触发该方法
protected void onCancelled() {
super.onCancelled();
}
3.完整代码
public class MainActivity extends AppCompatActivity {
//声明变量
private Button bnt1,bnt2;
private ProgressBar pog;
private TextView txt;
private PogTask pogTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
pogTask = new PogTask();
txt = findViewById(R.id.txt);
pog = findViewById(R.id.prog);
bnt1 = findViewById(R.id.bnt1);
bnt2 = findViewById(R.id.bnt2);
bnt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//启动异步任务类
if (pogTask.isCancelled()){//判断解决异常问题
pogTask = new PogTask();
}
pogTask.execute();
}
});
bnt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pogTask.cancel(true);
}
});
//pog.setProgress();
}
class PogTask extends AsyncTask<Void,Integer,String>{
@Override
//执行线程任务的页面操作,
protected void onPreExecute() {
super.onPreExecute();
txt.setText("加载中");
}
@Override
//不能处理UI操作,处理耗时操作(接受输入参数,返回线程执行结果)
protected String doInBackground(Void... voids) {
//模拟耗时操作(可用随机数)
try {
for (int i = 1; i <= 100; i++) {
publishProgress(i);
Thread.sleep(50); //模拟耗时操作
}
return "加载完毕";
}catch (InterruptedException e){
e.printStackTrace();
}
return null;
}
@Override
//在主线程中显示线程任务的执行进度,在doInBackground方法中调用publishProgress()方法会触发该方法
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//用数组
pog.setProgress(values[0]);
txt.setText("加载中"+values[0]+"%");
}
@Override
//接受线程任务的执行结果,将执行结果显示在页面上
protected void onPostExecute(String s) {
super.onPostExecute(s);
//判空
if (s != null){
txt.setText(s);
}
pogTask = new PogTask();
}
@Override
//取消(cancel)异步任务时触发该方法
protected void onCancelled() {
super.onCancelled();
txt.setText("已取消");
//并使进度条为0
pog.setProgress(0);
}
}
}
4.实现样式
那么就完成了简易的进度条,Android萌新一枚,有问题望大佬指出,谢谢。