Android 進度條對話框ProgressDialog


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="條形進度條" />

    <Button
        android:id="@+id/circle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圓形進度條" />

</LinearLayout>
package com.example.yanlei.my;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {

    //聲明按鈕
    private Button btnCircle=null;
    private Button btnProgress=null;
    //聲明進度條對話框
    private ProgressDialog pdDialog=null;
    //進度計數
    int iCount  = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnCircle = (Button)findViewById(R.id.circle);
        btnProgress = (Button)findViewById(R.id.progress);

        //設置btnCircle的事件監聽
        btnCircle.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v){

                iCount  = 0;
                //創建ProgressDialog對象
                pdDialog = new ProgressDialog(MainActivity.this);

                //設置進度條風格,風格為圓形,旋轉的
                pdDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

                // 設置ProgressDialog 標題
                pdDialog.setTitle("圓形進度條");

                // 設置ProgressDialog 提示信息
                pdDialog.setMessage("正在下載中……");

                // 設置ProgressDialog 標題圖標
                pdDialog.setIcon(R.drawable.ic_launcher);

                // 設置ProgressDialog 進度條進度
                pdDialog.setProgress(100);

                // 設置ProgressDialog 的進度條是否不明確
                pdDialog.setIndeterminate(false);

                // 設置ProgressDialog 是否可以按退回按鍵取消
                pdDialog.setCancelable(true);

                // 設置ProgressDialog 的一個Button
                pdDialog.setButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int i)
                    {
                        //點擊“取消”按鈕取消對話框
                        dialog.cancel();
                    }
                });

                // 讓ProgressDialog顯示
                pdDialog.show();

                //創建線程實例
                new Thread(){
                    public void run(){
                        try{
                            while (iCount  <= 100) {
                                // 由線程來控制進度。
                                pdDialog.setProgress(iCount ++);
                                Thread.sleep(50);
                            }
                            pdDialog.cancel();
                        }
                        catch (InterruptedException e){
                            pdDialog.cancel();
                        }
                    }

                }.start();
            }

        });



        //設置btnProgress的事件監聽
        btnProgress.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                iCount  = 0;
                // 創建ProgressDialog對象
                pdDialog = new ProgressDialog(MainActivity.this);

                // 設置進度條風格,風格為長形
                pdDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

                // 設置ProgressDialog 標題
                pdDialog.setTitle("條形進度條");

                // 設置ProgressDialog 提示信息
                pdDialog.setMessage("正在下載中……");

                // 設置ProgressDialog 標題圖標
                pdDialog.setIcon(R.drawable.ic_launcher);

                // 設置ProgressDialog 進度條進度
                pdDialog.setProgress(100);

                // 設置ProgressDialog 的進度條是否不明確
                pdDialog.setIndeterminate(false);

                // 設置ProgressDialog 是否可以按退回按鍵取消
                pdDialog.setCancelable(true);

                // 讓ProgressDialog顯示
                pdDialog.show();

                //創建線程實例
                new Thread(){
                    public void run(){
                        try{
                            while (iCount  <= 100) {
                                // 由線程來控制進度。
                                pdDialog.setProgress(iCount ++);
                                Thread.sleep(50);
                            }
                            pdDialog.cancel();
                        }
                        catch (InterruptedException e){
                            pdDialog.cancel();
                        }
                    }

                }.start();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM