布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.administrator.progress.MainActivity"> <ProgressBar android:id="@+id/progressBar" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>
实现:
package com.example.administrator.progress; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.ProgressBar; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar); final Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); progressBar.setProgress(msg.what); } }; final Thread thread = new Thread(){ @Override public void run() { super.run(); int index = 0; int numloop = 1; while (index < 100){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } handler.sendEmptyMessage(index); index++; if(index == 100){ while(index > 1){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } handler.sendEmptyMessage(index); index--; } } } } }; thread.start(); } }
转载:https://blog.csdn.net/wangbowj123/article/details/80454490