Android 基础控件之进度条ProgressBar的实现

本文主要介绍Android基础控件进度条ProgressBar的实现,具体操作看代码。

1.ProgressBarHandlerActivity.java

/**
 * 
 * 进度条ProgressBar与线程使用。单击开始按钮,进度条水平方向前进!
 *
 */
public class ProgressBarHandlerActivity extends Activity {
    /** Called when the activity is first created. */
	
	private Button startButton;
	private Button stopButton;
	private TextView result;
	private ProgressBar firstProgressBar;
	private final static int defaultValue=10;
	private int i=0;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progress_bar);
        setTitle("ProgressBar使用!");
        startButton=(Button)findViewById(R.id.startButton);
        stopButton=(Button)findViewById(R.id.stopButton);
        result=(TextView)findViewById(R.id.result);
        firstProgressBar=(ProgressBar)findViewById(R.id.firstProgressBar);
        
        firstProgressBar.setVisibility(0);//刚开始设置进度条可见
        
        startButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				/*i+=defaultValue;
				firstProgressBar.setProgress(i);
				firstProgressBar.setSecondaryProgress(i+defaultValue);*/
			//	result.setText(((double)i/firstProgressBar.getMax()*100)+"%");
				handler.post(progressBarThread);
			}
		});
        
        stopButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				handler.removeCallbacks(progressBarThread);			
			}
		});
    }
    
    //创建Handler对象
    Handler handler=new Handler();
    
    //创建线程对象
    MyRunnable progressBarThread=new MyRunnable();
    class MyRunnable implements Runnable{
		@Override
		public void run() {
			i+=defaultValue;
			handler.postDelayed(progressBarThread, 1000);
			firstProgressBar.setProgress(i);
			firstProgressBar.setSecondaryProgress(i+defaultValue);
			result.setText(((double)i/firstProgressBar.getMax()*100)+"%");
			if(i>=firstProgressBar.getMax()) i=0;
			
		}
    	
    }
    
}

2.布局文件progress_bar.xml

<?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"
    >
    
    <RelativeLayout
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	>
     	<ProgressBar
    		android:id="@+id/firstProgressBar"
    		android:layout_width="200px"
    		android:layout_height="wrap_content"
    		android:max="100"
    		style="?android:attr/progressBarStyleHorizontal"
  	  	/>
  	  	<TextView
  	  		android:id="@+id/result"
  	  		android:layout_width="wrap_content"
  	  		android:layout_height="wrap_content"
  	  		android:layout_toRightOf="@id/firstProgressBar"
  	  	/>
    </RelativeLayout>
    
    <RelativeLayout
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	>
    
    	<Button
    		android:id="@+id/startButton"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_centerHorizontal="true"
    		android:layout_alignParentRight="true"
    		android:layout_marginTop="10px"
    		android:paddingRight="10px"
    		android:paddingLeft="10px"
    		android:textColor="#0000AA"
    		android:textSize="15px"
    		android:text="开始"
    	/>
    	
    	<Button
    		android:id="@+id/stopButton"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_toLeftOf="@id/startButton"
    		android:layout_alignTop="@id/startButton"
    		android:paddingRight="10px"
    		android:textColor="#FF0000"
    		android:textSize="15px"
    		android:text="暂停"
    	/>

   </RelativeLayout>
    
</LinearLayout>

3. 注意:需要在AndroidManifest.xml注册相应Activity.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值