[Basic Andriod 讀書會] 2.1.5 製作處理進對話框(Progress Dialog)

摘要:[Basic Andriod] 2.1.5 製作處理進對話框(Progress Dialog)

Layout設定

<Button
android:id="@+id/btn_dialog3"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:text="Click to display a detailed progress dialog"
android:onClick="onClick3" />

程式

package com.example.progressdialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

	ProgressDialog progressDialog;
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void onClick3(View v) {
        showDialog(1);        
        progressDialog.setProgress(0);        
        
        new Thread(new Runnable(){
            public void run(){
                for (int i=1; i<=15; i++) {
                    try {
                        //---simulate doing something lengthy---
                        Thread.sleep(1000);
                        //---更新進度---
                        progressDialog.incrementProgressBy((int)(100/15));
                    } catch (InterruptedException e) {                    
                        e.printStackTrace();
                    }
                }
                progressDialog.dismiss();
            }
        }).start();
    }
    @Override
    protected Dialog onCreateDialog(int id) { 
        switch (id) {
        
            
        case 1:
            progressDialog = new ProgressDialog(this);
            progressDialog.setIcon(R.drawable.ic_launcher);
            progressDialog.setTitle("Downloading files...");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            //在對話框中加入OK Button
            progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", 
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                    int whichButton)
                    {
                        Toast.makeText(getBaseContext(),
                                "OK clicked!", Toast.LENGTH_SHORT).show();
                    }
            });
          //在對話框中加入Cancel Button
            progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                        int whichButton)
                    {
                        Toast.makeText(getBaseContext(),
                                "Cancel clicked!", Toast.LENGTH_SHORT).show();
                    }
            });
            return progressDialog;
        }
        
        return null;
    }
}

 

http://www.plurk.com/SophieQ