maybug

导航

Android开发之ProgressBar控件的使用

ProgressBar用于显示程序进行的进度

<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_height="wrap_content"
        android:layout_width="match_parent"
       
        style="?android:attr/progressBarStyleHorizontal"
        />
    <Button
        android:id="@+id/firstButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/ProgressBar"
        android:text="增加第一进度"/>
        <Button
        android:id="@+id/secondButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/firstButton"
        android:text="增加第二进度"/> 
        

</RelativeLayout>
package com.hhc.progressbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

    private ProgressBar progressBar;
    private Button firstButton;
    private Button secondButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        progressBar = (ProgressBar)findViewById(R.id.ProgressBar);
        firstButton = (Button)findViewById(R.id.firstButton);
        secondButton =(Button)findViewById(R.id.secondButton);
        progressBar.setMax(100);
        //progressBar.setProgress(20);
        //progressBar.setSecondaryProgress(50);
        firstButton.setOnClickListener(new firstListener());
        secondButton.setOnClickListener(new secondListener());
    }
    class firstListener implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            progressBar.incrementProgressBy(10);
        }
        
    }
    class secondListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            progressBar.incrementSecondaryProgressBy(10);;
        }
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

posted on 2013-11-21 21:45  maybug  阅读(295)  评论(0编辑  收藏  举报