首页 > 代码库 > android控件开发之progress

android控件开发之progress

android控件开发之progress

本博文主要讲述的是android开发中的progress进度条的使用方法,代码如下:

MainActivity.java:

package com.example.progress;


import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
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 firstBar = null;
private ProgressBar secendBar = null;
private Button myButton = null;
private int i = 0;  //记录bar的位置

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

firstBar = (ProgressBar)findViewById(R.id.firstBar);
secendBar = (ProgressBar)findViewById(R.id.secendBar);
myButton = (Button)findViewById(R.id.myButton);

//绑定监听器
myButton.setOnClickListener(new myBuutonSetOnclickListener());

}

class myBuutonSetOnclickListener implements OnClickListener{


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(i == 0){
//设置progress bar可见
firstBar.setVisibility(View.VISIBLE);
secendBar.setVisibility(View.VISIBLE);

}
else if(i > 0 && i < firstBar.getMax()){
//设置进度条的主进度的当前值
firstBar.setProgress(i);
//设置进度条的第二进度的当前值
firstBar.setSecondaryProgress(i + 10);

}
else{
firstBar.setVisibility(View.GONE);
secendBar.setVisibility(View.GONE);
firstBar.setProgress(0);
firstBar.setSecondaryProgress(0);
i = -10;
}

i += 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;
}


}

布局文件main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <ProgressBar
        android:id="@+id/firstBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="200"
        android:visibility="gone"
        style="?android:attr/progressBarStyleHorizontal"/>
    
    <ProgressBar 
        android:id="@+id/secendBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        style="?android:attr/progressBarStyle"/>
    
    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button"
        tools:ignore="HardcodedText" />

</LinearLayout>

显示的效果如下:


当点击按钮时,进度条会一步一步前进

android控件开发之progress