ProgressDialog类似于ProgressBar类。
用于显示一个过程,通常用于耗时操作。
几个方法介绍:
1.setMax()
设置对话框中进度条的最大值。
2.setTile()
设置标题。
3.setProgressStyl()
设置对话框中进度条的样式。例如:环形和水平。
参数:
ProgressDialog.STYLE_SPINNER 环形精度条
ProgressDialog.STYLE_HORIZONTAL 水平样式的进度条
4.setMessage()
设置显示的内容。
演示实例:
用于模拟一个耗时的连接操作。
图:
MainActivity.java
public class MainActivity extends Activity { Button button, button2; ProgressDialog progressDialog; public final static String ABOUT_SERVER = "Connect"; // 信息广播接收 BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context c, Intent intent) { String str = intent.getStringExtra("DATA"); if (str.equals("0")) { progressDialog.setMessage("开始连接"); } else if (str.equals("1")) { progressDialog.setMessage("连接完毕"); } else { progressDialog.dismiss(); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 注册广播监听 IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ABOUT_SERVER); registerReceiver(broadcastReceiver, intentFilter); } public void click(View v) { progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.setMessage("准备建立连接·············"); progressDialog.show(); Intent intent = new Intent(MainActivity.this, ServerActivity.class); startService(intent); } }activity_main.xml
<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" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/but" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="点击" /> </LinearLayout>ServerActivity.java
public class ServerActivity extends Service { Handler handler = new Handler() { public void handleMessage(Message msg) { Intent intent = new Intent(MainActivity.ABOUT_SERVER); if (msg.what == 0) { intent.putExtra("DATA", "0"); } else if (msg.what == 1) { intent.putExtra("DATA", "1"); } else { intent.putExtra("DATA", "2"); ServerActivity.this.stopSelf(); } sendBroadcast(intent); }; }; @Override public void onCreate() { super.onCreate(); Thread thread = new Thread(new Runnable() { @Override public void run() { // 要做的事 try { for (int i = 0; i < 3; i++) { Thread.sleep(1000); Message message = new Message(); message.what = i; handler.sendMessage(message); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); thread.start(); } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } }AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <service android:name="com.example.test.ServerActivity"></service> <activity android:name=".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> </application> </manifest>
final Message message = new Message(); Thread thread = new Thread(new Runnable() { @Override public void run() { // 要做的事 try { for (int i = 0; i < 3; i++) { Thread.sleep(1000); message.what = i; handler.sendMessage(message); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); thread.start();使用了同一个Message。用的API 19(4.4)与4.4模拟器,不显示错误,只是卡住,用的API 19(4.4)与2.2模拟器就报错了。
原文地址:http://blog.csdn.net/u011609853/article/details/45131711