Welcome 微信登录

首页 / 操作系统 / Linux / Android开发之进度条、拖动条、TabHost

进度条(ProgressBar)进度条可以带给用户良好的体验,Android系统已经为我们提供了ProgressBar类来完成进度条的效果,我们可以很方便的运用该类。其中最常见的两种进度条是“环形进度条”和“水平进度条”,在布局文件中定义两种进度条的方式比较相似,区别是,定义“水平进度条时”需要加上一项属性“style="?android:attr/progressBarStyleHorizontal"”。ProgressBar类中常用的方法如下:ProgressBar.setMax(int max);设置总长度为100ProgressBar.setProgress(int progress);设置已经开启长度为0,假设设置为50,进度条将进行到一半停止。实例:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >     <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="进度条演示" />     <ProgressBar        android:id="@+id/probarId1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:max="1000"        android:progress="100" />     <ProgressBar        android:id="@+id/probarId2"        style="@android:style/Widget.ProgressBar.Horizontal"       android:layout_marginTop="50dp"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:max="100000"        android:progress="100"         android:secondaryProgress="300"        /> </LinearLayout>////////////////// ProgressBarActivity///////////////////package cn.class3g.activity; import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.widget.ProgressBar; public class ProgressBarActivity extends Activity {   ProgressBar pro = null;   int i = 0;   int proMax = 0;    Handler handler = new Handler();    public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.main);      findViews();   }    private void findViews() {      pro = (ProgressBar) this.findViewById(R.id.probarId2);      proMax = pro.getMax();       new Thread(new Runnable() {         public void run() {           while (i < proMax) {              i=dowork();              handler.post(new Runnable() {                  public void run() {                    pro.setProgress(i);                  }              });           }         }       }       ).start();   }public int dowork(){   Log.d("tag", String.valueOf(i));   return i+=1;}   /*    * //不能用    *     * @Override public void run() { // TODO Auto-generated method stub while    * (i++ < proMax) { pro.setProgress(i); try { Thread.sleep(50); } catch    * (InterruptedException e) { // TODO Auto-generated catch block    * e.printStackTrace(); } } }    */}拖动条(SeekBar)进度条只能显示进度,用户不能与程序交互,通过对其操作来改变其值。而拖动条就可以实现此功能。拖动条比较常见,如“千千静听”中的播放进度条就是一个拖动条。Android平台中的SeekBar 是ProgressBar 的间接子类。SeekBar.getProgress():获取拖动条当前值调用setOnSeekBarChangeListener() 方法,处理拖动条值变化事件, 把SeekBar.OnSeekBarChangeListener 实例作为参数传入实例:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >     <SeekBar        android:layout_width="fill_parent"        android:layout_height="wrap_content"         android:max="1000"        android:id="@+id/seekbarId"        /> </LinearLayout>////////////////// SeekBarActivity/////////////////package cn.class3g.activity; import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener; public class SeekBarActivity extends Activity implements OnSeekBarChangeListener{   SeekBar seekbar = null;   public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.seekbar);      findViews();   }   private void findViews() {      seekbar = (SeekBar) this.findViewById(R.id.seekbarId);            seekbar.setOnSeekBarChangeListener(this);   }   @Override   public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {      Log.d("tag","changed:" +String.valueOf(arg0.getProgress()));         }   @Override   public void onStartTrackingTouch(SeekBar seekBar) {      Log.d("tag","start:" +String.valueOf(seekBar.getProgress()));         }   @Override   public void onStopTrackingTouch(SeekBar seekBar) {      Log.d("tag","stop:" +String.valueOf(seekBar.getProgress()));         }   }TabHost<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >     <LinearLayout        android:id="@+id/tab1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >       <Button        android:id="@+id/buttonId"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="切换到tab2" />     <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"         android:src="@drawable/p2"        android:scaleType="fitCenter"        android:layout_marginTop="20dp"        />  </LinearLayout></TabHost>/////////////// TabHostActivity///////////package cn.class3g.activity; import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TabHost; public class TabHostActivity extends TabActivity {   TabHost tabHost = null;    public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);       tabHost = this.getTabHost();       LayoutInflater inflater = LayoutInflater.from(this);       inflater.inflate(R.layout.tabhost, tabHost.getTabContentView(), true);       // tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator(""));      tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("切换标签")           .setContent(R.id.tab1));      tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("标签2")           .setContent(new Intent(this, ProgressBarActivity.class)));       findViews();   }    private void findViews() {      Button btn = (Button) this.findViewById(R.id.buttonId);      btn.setOnClickListener(new OnClickListener() {         @Override        public void onClick(View v) {           // tabHost.setCurrentTab(1);           tabHost.setCurrentTabByTag("tab2");         }      });    }}