简体   繁体   English

Android Progressbar在错误的时间启动

[英]Android Progressbar starts at the wrong time

I try to launch a progressbar in my application but wehn I launch it the BAr isn't show before the function is started 我尝试在我的应用程序中启动进度条,但是我们启动它后,在功能启动前BAr并未显示

public void onClick(View v) {

if (v == button)
{
    ProgressDialog dialog = ProgressDialog.show(App.this, "", 
            "Loading. Please wait...", true);
    dialog.show();
try
{

    directory = edittext.getText().toString();
    FileWriter fstream = new FileWriter("/data/data/folder.hide.alexander.fuchs/folder.db");
    BufferedWriter out = new BufferedWriter(fstream);
    out.write(directory);
    //Close the output stream
    out.close();
if(hide_or_show == "hide")
{



    edittext.setVisibility(View.INVISIBLE);
    folder_to_hide.setVisibility(View.INVISIBLE);
    hide();

    dialog.dismiss();
}
else
{   


    show();
    edittext.setVisibility(View.VISIBLE);
    folder_to_hide.setVisibility(View.VISIBLE);

    dialog.dismiss();
}
}
catch(Exception x)
{       
    String ErrorMessage = x.getMessage();
    Toast.makeText(this,"Error"+ErrorMessage, Toast.LENGTH_LONG).show();
    finish();
}
}
if (v == options)
{
    final CharSequence[] items = {"Change password", "http://www.alexander-fuchs.net/", "Market"};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Options");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            if (items[item] == "Change password")
            {
                createpass();

            }
            if (items[item] == "http://www.alexander-fuchs.net/")
            {
            intentstarter(items[item].toString());
           toaster(items[item].toString());
            }
            if (items[item] == "Market")
            {
            intentstarter("market://search?q=pub:Alexander Fuchs");
            toaster("Please wait...");
            }
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}
}

when I tap the button it takes long to respond and then the whole function finishs without prompting an progressbar 当我点击按钮时,它需要很长时间才能响应,然后整个功能完成而没有提示进度条

onClick is a callback where the return to Android is only returned when the callback ends. onClick是一个回调,仅在回调结束时才返回Android。 All UI interaction you do basically is collected and queued while the callback is active and executed after return (may not technically totally accurate). 您基本上完成的所有UI交互都会在回调处于活动状态并在返回之后执行(在技术上可能并不完全准确)时收集并排队。

For you ProgressBar to show up at the start of the action and vanish at the end, you can implement an AsyncTask where the progress bar is shown in onPreExecute , the real computation is done in doInBackground and the progressbar is dismissed in onPostExecute . 为了使ProgressBar在操作开始时显示并在操作结束时消失,您可以实现AsyncTask ,其中进度条显示在onPreExecute ,真正的计算在doInBackground完成,而进度条在onPostExecuteonPostExecute For example: 例如:

 protected void onPreExecute() {
        dialog = new ProgressDialog(context);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.show();
 }

 protected void onPostExecute(Map<Integer, String> integerStringMap) {
        if (dialog!=null)
            dialog.cancel();
 }

 protected void onProgressUpdate(Integer... values) {
        int val = values[0]*10000/num;
        dialog.setProgress(val);
 }

See here for the more complete example. 有关更完整的示例, 请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM