在对话框中按确定后,我应该从其他地方获取数据。所以我使用了 Asynctask 类。实现如下。但是在按第一次确定之后,我不会得到任何进度条。我只有在按下第二个 ok 后才得到它(基本上,当按钮 onClick 方法内的所有行都被执行时......我应该怎么做才能在按下第一个 ok 后立即获得进度条?
confirmPath= (Button) findViewById(R.id.confirmPath);
confirmPath.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
construction
AlertDialog.Builder builder = new AlertDialog.Builder(Destination.this);
builder.setMessage("Press 'Ok' ")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoadData task = new LoadData();
task.execute();
AlertDialog alertDialog = new AlertDialog.Builder(Destination.this).create();
alertDialog.setTitle("Attention!");
alertDialog.setMessage(" Pay attention");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing
}
});
builder.create();
builder.show();
这是 AsynkTask 类:
public class LoadData extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
//declare other objects as per your need
@Override
protected void onPreExecute()
{
progressDialog= ProgressDialog.show(Destination.this, "Progress Dialog Title Text","Please wait", true);
//do initialization of required objects objects here
};
@Override
protected Void doInBackground(Void... params)
{
ReadFromFile readFromFile= new ReadFromFile();
readFromFile.ReadAllData("Data");
//some other tasks to do
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
progressDialog.dismiss();
};
}