用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

android进度条功能实现

2013-07-05 作者: 武汉小强举报

[android]代码库

package com.example.handler;

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

public class MainActivity extends Activity {
	private ProgressBar progressBar;
	private SeekBar seekBar;
	private Button button1, button2, button3, button4, button5, button6;
	int i = 0;
	int j = 0;
	boolean flag1 = true;
	boolean flag2 = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		seekBar = (SeekBar) findViewById(R.id.seekBar);
		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		button3 = (Button) findViewById(R.id.button3);
		button4 = (Button) findViewById(R.id.button4);
		button5 = (Button) findViewById(R.id.button5);
		button6 = (Button) findViewById(R.id.button6);
		button1.setOnClickListener(new button());
		button2.setOnClickListener(new button());
		button3.setOnClickListener(new button());
		button4.setOnClickListener(new button());
		button5.setOnClickListener(new button());
		button6.setOnClickListener(new button());
		/**
		 * 拖动seekbar时间
		 */
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			/**
			 * seekbar拖动后的位置为第二个参数 arg1
			 */
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				// TODO Auto-generated method stub
				j=arg1;
			}
		});
	}

	/**
	 * 按钮点击事件
	 * 
	 * @author Administrator
	 * 
	 */
	class button implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.button1:
				if (flag1) {
					handler.post(rannable);
					flag1 = false;
				}
				break;
			case R.id.button2:
				flag1 = true;
				handler.removeCallbacks(rannable);
				break;
			case R.id.button3:
				flag1 = true;
				handler.removeCallbacks(rannable);
				i = 0;
				progressBar.setProgress(i);
				break;
			case R.id.button4:
				if (flag2) {
					
					handler2.post(r);
					flag2 = false;
				}
				break;
			case R.id.button5:
				flag2 = true;
				handler2.removeCallbacks(r);
				break;
			case R.id.button6:
				flag2 = true;
				handler2.removeCallbacks(r);
				j = 0;
				seekBar.setProgress(j);
				break;
			default:
				break;
			}
		}
	}

	/**
	 * 线程1
	 */
	Handler handler = new Handler();
	Runnable rannable = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			i++;
			progressBar.setProgress(i);

			handler.postDelayed(rannable, 100);
			if (i == 100) {
				i = 0;
			}
			System.out.println("ddd");
		}
	};
	/**
	 * 线程2
	 */
	Handler handler2 = new Handler();
	Runnable r = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			j++;
			seekBar.setProgress(j);
			handler2.postDelayed(r, 100);
			if (j == 100) {
				j = 0;
			}
			System.out.println("ooo");
		}
	};

	/**
	 * 菜单
	 */
	@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;
	}

}

[代码运行效果截图]


android进度条功能实现


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...