public class
ProgressBar
extends View
java.lang.Object | ||
↳ | android.view.View | |
↳ | android.widget.ProgressBar |
1. 实现了一个水平进度条
2. 使用线程 模拟真实进度情况
3. 从Thread中传值 在Handler中接收
4. ProgressDialog 实现
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start" /> <ProgressBar android:id="@+id/bar" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="30dp" android:paddingRight="30dp" android:paddingTop="15dp" android:visibility="gone" /> </LinearLayout>
java代码
@SuppressLint("ParserError") public class ProgressBarTest extends Activity { protected static final String TAG = "ProgressBarTest"; private ProgressBar bar = null; private Button btn = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PreInitUI(); EventInit(); dialogProgress(); } private void dialogProgress(){ final ProgressDialog mypDialog=new ProgressDialog(this); //实例化 mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); //设置进度条风格,风格为圆形,旋转的 mypDialog.setTitle("按Back取消Dialog"); //设置ProgressDialog 标题 mypDialog.setMessage(getResources().getString(R.string.hello_world)); //设置ProgressDialog 提示信息 mypDialog.setIcon(R.drawable.ic_launcher); //设置ProgressDialog 标题图标 mypDialog.setButton("Exit", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { mypDialog.dismiss(); } }); mypDialog.setIndeterminate(false); //设置ProgressDialog 的进度条是否不明确 mypDialog.setCancelable(true); //设置ProgressDialog 是否可以按退回按键取消 mypDialog.show(); //让ProgressDialog显示 } private void PreInitUI(){ setContentView(R.layout.progress_bar); bar = (ProgressBar)findViewById(R.id.bar); btn = (Button)findViewById(R.id.start); } private void EventInit(){ btn.setOnClickListener(listener); } private OnClickListener listener = new OnClickListener() { public void onClick(View v) { bar.setVisibility(View.VISIBLE); // 设置进度条可见 updateHandler.post(updateThread); // 第一次手动将线程放进处理器中 } }; /** * Handler 允许您发送和处理线程的MessageQueue的相关消息和Runnable对象的处理 * */:// //匿名内部类 private Handler updateHandler = new Handler(){ //handleMessage 返回arg1 arg2两个对象 以及额外一个字段域 public void handleMessage(Message msg) { bar.setProgress(msg.arg1); // 设置第一进度条进度 bar.setSecondaryProgress(msg.arg1+5);//设置地二进度条进度 Bundle bundle = msg.getData(); // 获得Message (除了arg1,arg2)之外的字段对象 String i = bundle.get("Extra").toString(); Toast.makeText(getApplication(), i+msg.arg1, Toast.LENGTH_SHORT).show(); updateHandler.post(updateThread); // 之后循环将线程 放进处理器中 } }; private Runnable updateThread = new Runnable() { int i = 0; public void run(){ i += 10; Message msg = updateHandler.obtainMessage(); msg.arg1 = i ; Bundle bundle = new Bundle(); bundle.putString("Extra", "Passing objects"); msg.setData(bundle); // 设置Message额外的字段 在处理器(Handler)中 得到这个对象 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if( i > 100){ // 处理器中 移除这个线程 updateHandler.removeCallbacks(updateThread); Log.v(TAG, ">>>>>>>>"); }else{ // 往处理器中发送Message对象 --->return sendMessageDelayed(msg, 0); updateHandler.sendMessage(msg); Log.v(TAG, "<<<<<<<<"); } } }; }
http://www.eoeandroid.com/thread-1081-1-1.html_______________________
系统所提供的进度条样式包括:@android:style/