gpt4 book ai didi

java - 简单的进度对话框 onClick 并在完成操作时隐藏

转载 作者:搜寻专家 更新时间:2023-11-01 08:54:03 25 4
gpt4 key购买 nike

我想在单击按钮时显示一个进度对话框(带有进度条或圆圈,这对我来说无关紧要)。其实我可以显示它,但我不知道如何在 onclick 完成内部操作后隐藏它。这是代码:

copy.setOnClickListener(new OnClickListener() {         
public void onClick(View v){

ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setProgressStyle(R.style.NewDialog);
progressDialog.setMessage("Loading...");
progressDialog.show();
String datafolder = Environment.getDataDirectory().getAbsolutePath()+File.separator+"app";
File customfolder=new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+File.separator+"BackupApps");
String comando = "cp -r /data/app /sdcard/BackupApps";
Process suProcess = null;
try {
suProcess = Runtime.getRuntime().exec("su");
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());

try {

os.writeBytes(comando + "\n");

} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();

}
try {
os.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.writeBytes("exit\n");

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.flush();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

}


});

我该怎么做?

最佳答案

首先在你的类中这样定义内部类

    class SaveTask extends AsyncTask<Void, Void, Void> {

ProgressDialog progressDialog;

@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(getActivity());
progressDialog.setProgressStyle(R.style.NewDialog);
progressDialog.setMessage("Loading...");
progressDialog.show();
}

@Override
protected Void doInBackground(Void... params) {
String datafolder = Environment.getDataDirectory()
.getAbsolutePath() + File.separator + "app";
File customfolder = new File(Environment
.getExternalStorageDirectory().getAbsolutePath().toString()
+ File.separator + "BackupApps");
String comando = "cp -r /data/app /sdcard/BackupApps";
Process suProcess = null;
try {
suProcess = Runtime.getRuntime().exec("su");
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
DataOutputStream os = new DataOutputStream(
suProcess.getOutputStream());

try {

os.writeBytes(comando + "\n");

} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();

}
try {
os.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.writeBytes("exit\n");

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.flush();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (progressDialog != null) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}

}

像这样在你的 onClick() 中调用它..这里我们没有向 AsynchTask 传递任何数据,所以这里我传递 null

copy.setOnClickListener(new OnClickListener() {         
public void onClick(View v){
SaveTask task = new SaveTask();
task.execute(null, null, null);
}

关于java - 简单的进度对话框 onClick 并在完成操作时隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20516960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com