ProgressDialog

Posted zqxLonely

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ProgressDialog相关的知识,希望对你有一定的参考价值。

package com.pingyijinren.helloworld.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.pingyijinren.helloworld.R;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        progressDialog=new ProgressDialog(this);
        progressDialog.setIcon(R.mipmap.ic_launcher);
        progressDialog.setTitle("标题");
        progressDialog.setMessage("内容");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "详细信息", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"你点击了详细信息",Toast.LENGTH_SHORT).show();
            }
        });
        progressDialog.show();

        new Thread(){
            @Override
            public void run() {
                super.run();
                for(int i=0;i<=100;i++){
                    Message message=new Message();
                    Bundle bundle=new Bundle();
                    bundle.putInt("i",i);
                    message.setData(bundle);
                    handler.sendMessage(message);

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle=msg.getData();
            int i=bundle.getInt("i");
            progressDialog.setProgress(i);
        }
    };
}

 

以上是关于ProgressDialog的主要内容,如果未能解决你的问题,请参考以下文章

当我旋转屏幕时,progressDialog 停止更新

打开新的 Fragment 和 AsyncTask 时显示 ProgressDialog

活动到片段方法调用带有进度条的线程

片段中的 JSON 解析 [关闭]

android中提示&对话框----ProgressDialog&DatePickerDialog &TimePickerDialog&PopupWindow(示例代码

ProgressDialog讲解