水平进度条ProgressBar(progressBarStyleHorizontal)

来源:互联网 发布:分布式 java 编辑:程序博客网 时间:2024/05/18 17:00



安卓开发需要的水平进度条ProgressBar(progressBarStyleHorizontal),点击进度条每秒钟增加进度10,进度加载完跳转页面
public class MainActivity extends Activity {
private ProgressBar progressBar_hor = null;
    private Button start = null, stop = null;
    private Handler handler = new Handler();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

progressBar_hor = (ProgressBar) findViewById(R.id.progressBar);
progressBar_hor.setProgress(0);

        start = (Button) findViewById(R.id.start);
        start.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {
                handler.post(runnable); //开始执行
            }
                
        });
        stop=(Button)findViewById(R.id.stop);
        stop.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {
                handler.removeCallbacks(runnable);//停止执行
                progressBar_hor.setProgress(0);
            }
                
        });
 
}

int pro=0;
   Runnable runnable=new Runnable(){
       public void run() {
        progressBar_hor.setVisibility(View.VISIBLE);
           pro=progressBar_hor.getProgress()+10;
           progressBar_hor.setProgress(pro);
           //如果进度小于100,,则延迟1000毫秒后重复执行runnable
           if(pro<100){
               handler.postDelayed(runnable, 1000);
           }else{
            progressBar_hor.setVisibility(View.GONE);
               startActivity(new Intent(MainActivity.this, TestActivity.class));
               handler.removeCallbacks(runnable);
               progressBar_hor.setProgress(0);
           }
       }
   };

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!-- 长方形进度条,一开始不可见,直到点击按钮时才出现进度条 -->
    <ProgressBar android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"      
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:max="100" />
    
    <Button android:id="@+id/start" 
        android:text="启动进度条"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button android:id="@+id/stop" 
        android:text="停止进度条"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 
    
</LinearLayout>

0 0