android ProgressBar用法

169 篇文章 4 订阅
package com.yunshouhu.interviewdemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {

	protected static final String TAG = "MainActivity";
	
	private ProgressBar progressBar = null;
	private TextView nameTextView = null;

	@SuppressLint("HandlerLeak")
	private Handler hander = new Handler() {

		public void handleMessage(android.os.Message msg) {
			

			switch (msg.what) {
			case 100:
			{
				int currentProgress =(Integer) msg.obj;
				nameTextView.setText(currentProgress+ "");
				Log.i(TAG, "handleMessage finish currentProgress="+currentProgress);
			}
				break;

			default:
				break;
			}

		};
	};

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

		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		nameTextView = (TextView) findViewById(R.id.name);

		new Thread(new Runnable() {

			@Override
			public void run() {

				try {
					//1分钟走完  1s走一次。 60s走完
					int progressBarMax = progressBar.getMax();
					Log.i(TAG, "progressBarMax="+progressBarMax);
					int stepProgress = progressBarMax / 60;
					
					while (progressBarMax != progressBar.getProgress()) {
						
						int currentProgress = progressBar.getProgress();
						
						int nextProgress=currentProgress + stepProgress;
						progressBar.setProgress(nextProgress);

					
						Message msg=hander.obtainMessage();
						msg.what=100;
						msg.obj=nextProgress;
						hander.sendMessage(msg);

						Log.i(TAG, "send finish currentProgress="+nextProgress);
						Thread.sleep(1000);

					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}).start();

	}

}

<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">

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:textSize="18sp"
        android:text="@string/hello_world" />
    
     <ProgressBar
        android:id="@+id/progressBar"
         style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:max="120"
        android:secondaryProgress="90" />
</RelativeLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值