【问题标题】:where and how to start and stop progress bar in my code在我的代码中在哪里以及如何开始和停止进度条
【发布时间】:2020-05-11 16:36:44
【问题描述】:

我有一个翻译应用程序工作正常,但我想在用户按下翻译按钮时对其实施进度条,进度条应该启动并且可见,一旦翻译完成,进度条应该停止并且不可见。

下面是我的翻译按钮代码方法

     translateBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkInternetConnection()) {

                //if there is internet connection, get translate service and start translation.
                getTranslateService();
                translate();

            } else {

                //if not display "no internet connection" warning
                Toast.makeText(MainActivity.this, "Error! please check your internet connection and try again!!", Toast.LENGTH_LONG).show();

            }
            progressBar.setVisibility(View.VISIBLE);
        }
    });

我已经在我的代码中设置了进度条,但是当翻译完成时它一直在旋转并且不会停止。我很困惑将可见性和不可见代码行放在哪里。

【问题讨论】:

    标签: android progress-bar translation google-translate


    【解决方案1】:

    您可以使用这些方法来显示和隐藏进度对话框。在需要的地方调用这些方法。

     ProgressDialog progressDialog;
        public void showProgressDialog(String title, String msg, Context context) {
            progressDialog.setTitle (title);
            progressDialog.setMessage (msg);
            progressDialog.setCancelable (false);
            progressDialog.show ();
        }
    
        public void hideProgressDialog() {
            if (progressDialog != null) {
                if (progressDialog.isShowing ()) {
                    progressDialog.dismiss ();
                }
            }
        }
    

    根据您的代码透视图调用这些方法,如下所示。

     translateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkInternetConnection()) {
                  showProgressDialog(Your title,Your message,getContext);
    
                 //if there is internet connection, get translate service and start translation.
                   getTranslateService();
                    translate();
                    hideProgressDialog();
                } else {
    
                    //if not display "no internet connection" warning
                    Toast.makeText(MainActivity.this, "Error! please check your internet connection and try again!!", Toast.LENGTH_LONG).show();
    
                }
    
            }
        });
    

    此外,如果您想显示进度条,请这样做

     translateBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (checkInternetConnection()) {
                       progressBar.setVisibility(View.VISIBLE);
    
                     //if there is internet connection, get translate service and start translation.
                       getTranslateService();
                        translate();
                        progressBar.setVisibility(View.GONE);
    
                    } else {
    
                        //if not display "no internet connection" warning
                        Toast.makeText(MainActivity.this, "Error! please check your internet connection and try again!!", Toast.LENGTH_LONG).show();
    
                    }
    
                }
            });
    

    【讨论】:

      猜你喜欢
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多