Android Progressbar在错误的时间启动

时间:2012-02-09 18:44:08

标签: java android function progress-bar

我尝试在我的应用程序中启动一个进度条但是我在启动它之前启动了BAr isn #t才显示该功能

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();
}
}

当我点击按钮时,它需要很长时间才能响应,然后整个功能完成而不会提示进度条

1 个答案:

答案 0 :(得分:1)

onClick是一个回调,只有在回调结束时才返回Android。 在回调处于活动状态时,您所做的所有UI交互基本上都会被收集和排队,并在返回后执行(技术上可能完全不准确)。

为了让ProgressBar在操作开始时显示并在结束时消失,您可以实现AsyncTask,其中进度条显示在onPreExecute中,实际计算在{ {1}}并且doInBackground中的进度条被取消。例如:

onPostExecute

See here获取更完整的示例。