完成任务后如何显示Toast?

时间:2017-03-16 18:57:20

标签: java android-studio

我使用此代码删除文件

File dir = new File(Environment.getExternalStorageDirectory() + "/myapp/" + "/Media/");
if (dir.isDirectory()) {
    String[] children = dir.list();
    for (int i = 0; i < children.length; i++) {
        new File(dir, children[i]).delete();

    }

    Toast.makeText(appActivity.this, "done", Toast.LENGTH_SHORT).show();

}

我想展示Toast&#34;完成&#34;从文件夹和其他Toast&#34; No file&#34;删除文件后如果文件夹中有o文件。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

你需要的是if else声明, 有关if else语句的更多帮助,请阅读Android开发人员文档https://developer.android.com/reference/java/util/concurrent/locks/Condition.html

试试这个;

  File dir = new File(Environment.getExternalStorageDirectory() + "/myapp/" + "/Media/");
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
    new File(dir, children[i]).delete();

}

Toast.makeText(appActivity.this, "done", Toast.LENGTH_SHORT).show();

else {
Toast.makeText(appActivity.this, "no file!", Toast.LENGTH_SHORT).show();
}

}

答案 1 :(得分:0)

我认为这应该有用

File dir = new File(Environment.getExternalStorageDirectory() + "/myapp/" + "/Media/");
if (dir.isDirectory()) {
    String[] children = dir.list();
//if there are files to delete inside the folder
    if(children.length>0){
        Toast.makeText(appActivity.this, "done", Toast.LENGTH_SHORT).show();
    }
//if there are no files inside the folder
    else{
        Toast.makeText(appActivity.this, "No files to delete", Toast.LENGTH_SHORT).show(); 
    }
    for (int i = 0; i < children.length; i++) {
        new File(dir, children[i]).delete();

    }



}
else{
    Toast.makeText(appActivity.this, "No Such directory exists", Toast.LENGTH_SHORT).show();
}

希望这有帮助