AsyncTask的简单使用:点击加载按钮后,进度条进行加载,到100%后界面文字改变,进度条隐藏。
一、效果预览图
二、代码实现
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="MainActivity">
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginHorizontal="20dp"
android:text="Click the button to start."
android:textSize="20dp"/>
<ProgressBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="20dp"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:visibility="visible"/>
<Button
android:id="@+id/btn_downLoad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="50dp"
android:text="download" />
</LinearLayout>
- MainActvity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ProgressBar bar;
private TextView tv_show;
private Button btn_downLoad;
private load download;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar) findViewById(R.id.bar);
tv_show = (TextView) findViewById(R.id.tv_show);
btn_downLoad = (Button) findViewById(R.id.btn_downLoad);
//set the onClickListener to the button
btn_downLoad.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
download = new load();
download.execute();
}
});
}
//define an AsyncTask
private class load extends AsyncTask<String, Integer, Void>
{
//Begin-can use UI thread here
@Override
protected void onPreExecute()
{
super.onPreExecute();
tv_show.setText("Some important data is being collected now.\n"
+"Please be patient and wait...");
}
//this is the slow background thread taking care of heavy tasks
//cannot directly change UI
@Override
protected Void doInBackground(final String... args )
{
for (int i = 1; i <= 100; i++) {
// refresh the data
publishProgress(i);
if(isCancelled()){
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
//periodic updates- it is ok to change UI
@Override
protected void onProgressUpdate(Integer... values)
{
super.onProgressUpdate(values[0]);
bar.setProgress(values[0]);
if(isCancelled())
{
return;
}
tv_show.setText("loading..." + values[0] + "%");
}
// End -can use UI thread here
@Override
protected void onPostExecute(final Void result)
{
super.onPostExecute(result);
tv_show.setText("The data is downloaded.");
bar.setVisibility(View.GONE);
}
}
}