• Android学习之ProgressBar


    ProgressBar用于向用户显示某个耗时操作完成的百分比,避免长时间执行某个耗时操作时让用户感觉程序失去了响应,从而提高用户界面的友好性。
    请看下面的界面布局:
    <LinearLayout 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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@android:style/Widget.ProgressBar.Large"
    />
    <ProgressBar
    android:id="@+id/bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:max="100"
    style="@android:style/Widget.ProgressBar.Horizontal"
    />
    </LinearLayout>
    上面的界面布局定义了2个ProgressBar组件,第1个为大环形,其style属性为@android:style/Widget.ProgressBar.Large,这种环形进度条默认无法显示进度,只是显示一个不断旋转的图片(显示进度的环形进度条可以参考园子里的环形进度条实现http://www.cnblogs.com/kimmy/p/4833321.html。)
    第2个水平进度条,其style属性为@android:style/Widget.ProgressBar.Horizontal,该进度条的最大值为100。
    接下来用一个填充数组的任务模拟耗时操作,用进度条来标识任务的完成百分比。
    public class MainActivity extends Activity {
    	private int[] data=new int[100];
    	int hasData=0;
    	int status=0;
    	ProgressBar bar;
    	Handler handler=new Handler()
    	{
    		@Override
    		public void handleMessage(Message msg)
    		{
    			if(msg.what==0x11)
    			{
    				bar.setProgress(status);
    			}
    		}		
    	};
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		bar=(ProgressBar)findViewById(R.id.bar);
    		new Thread()
    		{
    
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				while (status<100) {
    					status=doWork();
    					handler.sendEmptyMessage(0x11);		
    				}			
    			}		
    		}.start();
    	}
    	
    	private int doWork() {
    		data[hasData++]=(int)(Math.random()*100);
    		try {
    			Thread.sleep(100);
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		}
    		return hasData;
    	}
    
    	@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;
    	}
    
    }
    

      

  • 相关阅读:
    pyc文件是什么【转载】
    Linux下的python等操作【转载】
    P1012 拼数 字符串
    P1309 瑞士轮 排序选择 时间限制 归并排序
    商业竞争 三分+背包
    老虎ji 剪枝模拟
    交通灯 并查集
    三色抽卡游戏 博弈论nim
    质数串 乱搞
    自动驾驶系统 bfs
  • 原文地址:https://www.cnblogs.com/zhouhb/p/5813101.html
Copyright © 2020-2023  润新知