如何在进度栏中显示AlertDialog?

时间:2019-10-24 09:12:51

标签: java android android-studio progress-bar android-alertdialog

  

我不知道如何在进度条显示时显示AlertDialog   例如达到22%。

我希望进度条达到22%时停止并显示Al e rtDialog窗口,其中显示一些文本和默认按钮,例如“确定”和“取消”。当您按OK时,进度条将恢复并继续进行进度加载。直到完成进度条的100%为止。

问题是我不知道如何停止进度条,例如22%并显示AlertDialog。所以这是我的疑问。怎么做才是正确的方法。

这是我到目前为止所做的,在此先感谢同事;)

MainActivity

public class MainActivity extends AppCompatActivity {

private ProgressBar progressBar4;
private int progressStatus4 = 0;
private Handler handler = new Handler();


    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                carga();
            }
        });
    }

    private void carga(){
        final Dialog dialogCorrect = new Dialog(MainActivity.this);
        dialogCorrect.requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (dialogCorrect.getWindow() != null) {
            ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
            dialogCorrect.getWindow().setBackgroundDrawable(colorDrawable);
        }
        dialogCorrect.setContentView(R.layout.dialog);
        dialogCorrect.setCancelable(false);
        dialogCorrect.show();

        final TextView tv44 = dialogCorrect.findViewById(R.id.tv44);
        progressBar4 = dialogCorrect.findViewById(R.id.pb44);
        final TextView txt = dialogCorrect.findViewById(R.id.txt);
        txt.setText("Connect");

        if (progressStatus4 == 100) {
            progressStatus4 = 0; //Reset Values
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (progressStatus4 < 100) {

                    progressStatus4 += 1;

                    try {
                         Thread.sleep(200);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                if(progressBar4.getProgress() >= 20 && progressBar4.getProgress() <= 41){
                                    txt.setText("Update"); 

                                }else if(progressBar4.getProgress() >= 41 && progressBar4.getProgress() <= 71){
                                    txt.setText("GET");

                                }else if(progressBar4.getProgress() >= 71 && progressBar4.getProgress() <= 99){
                                    txt.setText("WHAIT");

                                }else if(progressBar4.getProgress() >= 99 && progressBar4.getProgress() <= 100){
                                    dialogCorrect.dismiss(); //Close PopUp


                                }
                            }
                        });
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }


                    // Update the progress bar
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            progressBar4.setProgress(progressStatus4);
                            progressBar4.setSecondaryProgress(progressStatus4 + 15);
                            // Show the progress on TextView
                            tv44.setText(progressStatus4 + "%");
                        }
                    });


                }
            }
        }).start(); // Start the operation
    }
}

0 个答案:

没有答案