public class MainActivity extends AppCompatActivity { private ProgressBar progressBar; private Button btnDownLoad; private TextView txtView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { progressBar = findViewById(R.id.progressBar); btnDownLoad = findViewById(R.id.btnDownLoad); txtView = findViewById(R.id.txtView); } public void onDownLoad(View view) { new DownLoadTask().execute(20); } class DownLoadTask extends AsyncTask{ @Override protected void onPreExecute() { txtView.setText("下载中。。。"); } @Override protected Boolean doInBackground(Integer...integers) { int progress = integers[0]; while (true) { progress+=5; publishProgress(progress); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } if (progress > 100) { return true; } } } @Override protected void onProgressUpdate(Integer... values) { int progress = values[0]; progressBar.setProgress(progress); } @Override protected void onPostExecute(Boolean aBoolean) { if (aBoolean) { txtView.setText("下载完毕!"); } } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)