感觉没什么说的,就直接放代码吧
代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package com.example.administrator.prograssbarandthread;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private Button button; private ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.Button); progressBar = (ProgressBar)findViewById(R.id.ProgressBar); button.setOnClickListener(new ButtonListener()); }
class ButtonListener implements View.OnClickListener{ @Override public void onClick(View view){ MyThread thread = new MyThread(); thread.start(); } }
class MyThread extends Thread{ @Override public void run(){ for (int i=0;i<=100;i++){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } progressBar.setProgress(progressBar.getProgress() + 1); } } } }
|
不过关于ProgressBar的具体使用在下面地址里有
https://developer.android.com/reference/android/widget/ProgressBar.html