Android 进度条
**Android 前端代码**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:id="@+id/tv_main_tv1"
android:layout_width="60dp"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/pb_main_pb1"
android:layout_width="match_parent"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress="10"//从几开始
android:layout_height="70dp" />
</FrameLayout>
<Button
android:layout_width="100dp"
android:id="@+id/main_bnt_bnt"
android:onClick="setProgress"
android:layout_height="100dp"
android:text="bbbb"/>
</LinearLayout>
**Android 后台代码**
package com.example.t212_a05;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ProgressBar pb_main_pb1;
private int progress;
private TextView tv_main_tv1;
private Myhandler myHandler = new Myhandler();
private int code = 1;
private class Myhandler extends Handler{
public void handleMessage(Message msg){
super.handleMessage(msg);
if(code == msg.what){
progress++;
pb_main_pb1.setProgress(progress);
tv_main_tv1.setText(progress+"%");
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb_main_pb1 = this.findViewById(R.id.pb_main_pb1);
tv_main_tv1 = this.findViewById(R.id.tv_main_tv1);
}
public void studyToast(View view){
Toast.makeText(this, "Holle", Toast.LENGTH_LONG).show();
}
public void setProgress(View view){
if(0 == progress){
new MyThread().start();//开始线程
}
}
private class MyThread extends Thread{
@Override
public void run(){
super.run();
while(true){
SystemClock.sleep(100);
if(progress == 100){
progress = 0;
break;
}
Message msg = new Message();
msg.what = 1;
myHandler.sendMessage(msg);
}
}
}
}