对话框中的拖动条
package com.ko8e; import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MyActivity extends Activity { /** Called when the activity is first created. */ private Button button1 = null; private Button button2 = null; private ProgressDialog pDialog = null; int count = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { pDialog = new ProgressDialog(MyActivity.this); pDialog.setIcon(R.drawable.img1); pDialog.setTitle("提示"); pDialog.setMessage("这是一个圆形的进度条"); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); pDialog.setButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { MyActivity.this.finish(); } }); pDialog.show(); } }); button2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { count = 0; pDialog = new ProgressDialog(MyActivity.this); pDialog.setIcon(R.drawable.img2); pDialog.setTitle("提示"); pDialog.setMessage("这是一个长形进度条"); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pDialog.setMax(100); pDialog.show(); new Thread(new Runnable() { public void run() { try { while (count <= 100) { Thread.sleep(100); pDialog.setProgress(count++); } pDialog.cancel(); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } }); } }
?main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button2" /> </LinearLayout>?