package com.yunshouhu.interviewdemo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
private ProgressBar progressBar = null;
private TextView nameTextView = null;
@SuppressLint("HandlerLeak")
private Handler hander = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 100:
{
int currentProgress =(Integer) msg.obj;
nameTextView.setText(currentProgress+ "");
Log.i(TAG, "handleMessage finish currentProgress="+currentProgress);
}
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
nameTextView = (TextView) findViewById(R.id.name);
new Thread(new Runnable() {
@Override
public void run() {
try {
//1分钟走完 1s走一次。 60s走完
int progressBarMax = progressBar.getMax();
Log.i(TAG, "progressBarMax="+progressBarMax);
int stepProgress = progressBarMax / 60;
while (progressBarMax != progressBar.getProgress()) {
int currentProgress = progressBar.getProgress();
int nextProgress=currentProgress + stepProgress;
progressBar.setProgress(nextProgress);
Message msg=hander.obtainMessage();
msg.what=100;
msg.obj=nextProgress;
hander.sendMessage(msg);
Log.i(TAG, "send finish currentProgress="+nextProgress);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:textSize="18sp"
android:text="@string/hello_world" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max="120"
android:secondaryProgress="90" />
</RelativeLayout>