当一个应用程序在后台执行时,前台界面不会产生变化,但因为 Android 各机型配置大不相同,有时执行程序的过程中用户不知道发生了什么事,但界面却发生了变化。这时需要与用户交互的进度条用来提示用户后台执行程序的进度,这种做法是符合人性化的,进度条充分的符合上面的需求。进度条的详解如下:
1、进度条风格
2、进度条主要属性方法
3、模拟程序运行,使用进度条
1、进度条分类
长形进度条 (progressBarStyleHorizontal)
大圆形进度条(progressBarStyleLarge)
小圆形进度条 (progressBarStyleSmall)
默认风格 (progressBarStyle)
package com.itarchy.bar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ProgreBar
button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
ProgreBarActivity.class));
}
});
//ProgreDialog
button2 = (Button) this.findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
ProgreDialogActivity.class));
}
});
//RatingBar
button3 = (Button) this.findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
RatingBarActivity.class));
}
});
//SeekBar
button4 = (Button) this.findViewById(R.id.button4);
button4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
SeekBarActivity.class));
}
});
}
}
<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"
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" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dip"
android:text="各种进度条"
android:textSize="25sp" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginTop="15dip"
android:gravity="center_horizontal"
android:text="ProgressBar"
android:textSize="25sp" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="15dip"
android:gravity="center_horizontal"
android:text="ProgressDailog"
android:textSize="25sp" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_marginTop="15dip"
android:gravity="center_horizontal"
android:text="RatingBar"
android:textSize="25sp" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:layout_marginTop="15dip"
android:gravity="center_horizontal"
android:text="SeekBar"
android:textSize="25sp" />
</RelativeLayout>
(一)
当在加载数据到litView或者其他控件上时,用户会有一个等待的时间,为了给用户一个友好的提示,通常我们会显示一个ProgressBar,待数据加载完毕后,ProgressBar自动消失。
package com.itarchy.bar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ProgressBar;
public class ProgreBarActivity extends Activity {
private ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS); // 进度指示器功能
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.progress);
// 标题栏显示不确定的进度的进度条
setProgressBarIndeterminateVisibility(true);
}
}
(二)执行某个耗时操作时,为了安抚用户等待的烦躁心情我们一般会使用进度条之类的空间,在android中让大家最 容易想到的就是progressbar或者progressDialog,区别在于前者是一个控件,后者是对话框。由于一些需求在弹出进度条时不希望用户 能够操作其他控件,所以只能使用progressDialog,这个时候有遇到了一个问题,我不想要progressDialog的黑色框框,感觉这样跟 应用的整体风格不协调,这个时候就考虑了写一个自定义的progressDialog。
package com.itarchy.bar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;
public class ProgreDialogActivity extends Activity {
private ProgressDialog pdialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pdialog = ProgressDialog.show(ProgreDialogActivity.this, "加装网络",
"努力加装中...");
// 3000之后,执行run()函数
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
pdialog.hide();
}
}, 3000);
}
}
<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:orientation="vertical"
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" >
<RatingBar
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="6"
android:stepSize="1" />
<RatingBar
android:id="@+id/rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="3"
android:rating="1"
android:layout_gravity="center_horizontal"
android:stepSize="0.5" />
</LinearLayout>
(三)
拖动条(SeekBar)组件与ProgressBar水平形式的显示进度条类似,不过其最大的区别在于,拖动条可以由用户自己进行手工的调节,例如当用户需要调整播放器音量或者电影的播放速度都会使用到拖动条SeekBar类。
package com.itarchy.bar;
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 {
private SeekBar mSeekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar);
mSeekBar = (SeekBar) this.findViewById(R.id.seekbar);
// 对SeekBar控件监听
mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.i("TAG", "SeekBar--onStopTrackingTouch");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.i("TAG", "SeekBar--onStartTrackingTouch");
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
Log.i("TAG", "SeekBar--onProgressChanged");
Log.i("TAG", "SeekBar--progress=" + progress);
Log.i("TAG", "SeekBar--fromUser=" + fromUser);
}
});
}
}
<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:orientation="vertical"
android:background="#000000"
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" >
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="10"
android:maxHeight="3px"
android:progressDrawable="@drawable/seekbar_bg"
android:thumbOffset="5px"
android:thumb="@drawable/thumb_ttpod" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itarchy.bar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itarchy.bar.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.itarchy.bar.ProgreBarActivity"
android:theme="@android:style/Theme.Black" />
<activity android:name="com.itarchy.bar.SeekBarActivity" />
<activity android:name="com.itarchy.bar.ProgreDialogActivity" />
<activity android:name="com.itarchy.bar.RatingBarActivity" />
</application>
</manifest>