这是mainActivity.java
package main.i_love_luoxiao_05;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;public class MainActivity extends Activity {
   private ProgressBar bar;
   private Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar)findViewById(R.id.progressBar);
startButton = (Button)findViewById(R.id.myButton);
startButton.setOnClickListener(new ButtonListener());
} class ButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
bar.setVisibility(View.VISIBLE);
handler.post(update);
}

}

Handler handler = new Handler();
public void handleMessage(Message msg){
bar.setProgress(msg.arg1);
handler.post(update);
}
Runnable update = new Runnable() {

int i = 0;
@Override
public void run() {
// TODO Auto-generated method stub
i = i+10;
Message msg =handler.obtainMessage();
msg.arg1 = i;
try{
Thread.sleep(1000);
}catch (InterruptedException o){
o.printStackTrace();
}
handler.sendMessage(msg);
if(i==100)
handler.removeCallbacks(update);
}
};

}
这个是xml

<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"
    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=".MainActivity" >    <ProgressBar 
        android:id="@+id/progressBar"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:visibility="gone"
        android:max="100"
        />
    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/progressBar"/></RelativeLayout>

解决方案 »

  1.   

    package com.example.dialog;import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ProgressBar;public class MainActivity extends Activity {
    private ProgressBar bar;
    private Button startButton;
    Handler handler = new Handler(){ @Override
    public void handleMessage(Message msg) {
    // TODO Auto-generated method stub
    super.handleMessage(msg);
    bar.setProgress(msg.arg1);
    handler.post(update);
    }

    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bar = (ProgressBar) findViewById(R.id.progressBar);
    startButton = (Button) findViewById(R.id.myButton);
    startButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    bar.setVisibility(View.VISIBLE);
    handler.post(update);
    }
    });
    } Runnable update = new Runnable() { int i = 0; @Override
    public void run() {
    // TODO Auto-generated method stub
    i = i + 10;
    Message msg = handler.obtainMessage();
    msg.arg1 = i;
    try {
    Thread.sleep(1000);
    } catch (InterruptedException o) {
    o.printStackTrace();
    }
    handler.sendMessage(msg);
    if (i == 100)
    handler.removeCallbacks(update);
    }
    };}不知道这样符合不符合你的要求,改了一下你的代码!