2013-04-03 61 views
2

對話框我有完全相同的問題,因爲這個帖子 Choose File DialogAndroid的文件再次選擇

我沒有看到有什麼真正可用的答案。

理想我想一個簡單的對話框,就像MFC CFileDialog類的一個簡單的Java代碼示例,即您只需新的吧,做一個.show和它的作品

一切,所以我見過看起來過於複雜的應該是一個非常簡單的工作。任何人都可以幫助更多最新的信息?

感謝

UPDATE,

在該職位

Choose File Dialog 由基里爾·米哈伊洛夫答案似乎幾分鐘測試

回答

1

後工作正常我添加了一個取消按鈕,該示例中,似乎工作正常

import java.io.File; 
import java.io.FilenameFilter; 
import java.util.ArrayList; 
import java.util.List; 

import com.design2112.tabtrax.ListenerList.FireHandler; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Environment; 

public class FileDialog { 
    private static final String PARENT_DIR = ".."; 
    private final String TAG = getClass().getName(); 
    private String[] fileList; 
    private File currentPath; 

    public interface FileSelectedListener { 
     void fileSelected(File file); 
    } 

    public interface DirectorySelectedListener { 
     void directorySelected(File directory); 
    } 

    private ListenerList<FileSelectedListener> fileListenerList = new ListenerList<FileDialog.FileSelectedListener>(); 
    private ListenerList<DirectorySelectedListener> dirListenerList = new ListenerList<FileDialog.DirectorySelectedListener>(); 
    private final Activity activity; 
    private boolean selectDirectoryOption; 
    private String fileEndsWith; 

    /** 
    * @param activity 
    * @param initialPath 
    */ 
    public FileDialog(Activity activity, String path) { 
     this.activity = activity; 
     loadFileList(new File(path)); 
    } 

    /** 
    * @return file dialog 
    */ 
    public Dialog createFileDialog() { 
     Dialog dialog = null; 
     AlertDialog.Builder builder = new AlertDialog.Builder(activity); 

     builder.setTitle(currentPath.getPath()); 
     /* 
     * if (selectDirectoryOption) { 
     * builder.setPositiveButton("Select directory", new OnClickListener() { 
     * public void onClick(DialogInterface dialog, int which) { Log.d(TAG, 
     * currentPath.getPath()); fireDirectorySelectedEvent(currentPath); } 
     * }); } 
     */ 

     builder.setItems(fileList, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       String fileChosen = fileList[which]; 
       File chosenFile = getChosenFile(fileChosen); 
       if (chosenFile.isDirectory()) { 
        loadFileList(chosenFile); 
        dialog.cancel(); 
        dialog.dismiss(); 
        showDialog(); 
       } else 
        fireFileSelectedEvent(chosenFile); 
      } 
     }); 
     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
     }); 
     dialog = builder.show(); 
     return dialog; 
    } 

    public void addFileListener(FileSelectedListener listener) { 
     fileListenerList.add(listener); 
    } 

    public void removeFileListener(FileSelectedListener listener) { 
     fileListenerList.remove(listener); 
    } 

    public void setSelectDirectoryOption(boolean selectDirectoryOption) { 
     this.selectDirectoryOption = selectDirectoryOption; 
    } 

    public void addDirectoryListener(DirectorySelectedListener listener) { 
     dirListenerList.add(listener); 
    } 

    public void removeDirectoryListener(DirectorySelectedListener listener) { 
     dirListenerList.remove(listener); 
    } 

    /** 
    * Show file dialog 
    */ 
    public void showDialog() { 
     createFileDialog().show(); 
    } 

    private void fireFileSelectedEvent(final File file) { 
     fileListenerList.fireEvent(new FireHandler<FileDialog.FileSelectedListener>() { 
      public void fireEvent(FileSelectedListener listener) { 
       listener.fileSelected(file); 
      } 
     }); 
    } 

    private void fireDirectorySelectedEvent(final File directory) { 
     dirListenerList.fireEvent(new FireHandler<FileDialog.DirectorySelectedListener>() { 
      public void fireEvent(DirectorySelectedListener listener) { 
       listener.directorySelected(directory); 
      } 
     }); 
    } 

    private void loadFileList(File path) { 
     this.currentPath = path; 
     List<String> r = new ArrayList<String>(); 
     if (path.exists()) { 
      if (path.getParentFile() != null) 
       r.add(PARENT_DIR); 
      FilenameFilter filter = new FilenameFilter() { 
       public boolean accept(File dir, String filename) { 
        File sel = new File(dir, filename); 
        if (!sel.canRead()) 
         return false; 
        if (selectDirectoryOption) 
         return sel.isDirectory(); 
        else { 
         boolean endsWith = fileEndsWith != null ? filename 
           .toLowerCase().endsWith(fileEndsWith) : true; 
         return endsWith || sel.isDirectory(); 
        } 
       } 
      }; 
      String[] fileList1 = path.list(filter); 
      for (String file : fileList1) { 
       r.add(file); 
      } 
     } 
     fileList = (String[]) r.toArray(new String[] {}); 
    } 

    private File getChosenFile(String fileChosen) { 
     if (fileChosen.equals(PARENT_DIR)) 
      return currentPath.getParentFile(); 
     else 
      return new File(currentPath, fileChosen); 
    } 

    public void setFileEndsWith(String fileEndsWith) { 
     this.fileEndsWith = fileEndsWith != null ? fileEndsWith.toLowerCase() 
       : fileEndsWith; 
    } 
} 

class ListenerList<L> { 
    private List<L> listenerList = new ArrayList<L>(); 

    public interface FireHandler<L> { 
     void fireEvent(L listener); 
    } 

    public void add(L listener) { 
     listenerList.add(listener); 
    } 

    public void fireEvent(FireHandler<L> fireHandler) { 
     List<L> copy = new ArrayList<L>(listenerList); 
     for (L l : copy) { 
      fireHandler.fireEvent(l); 
     } 
    } 

    public void remove(L listener) { 
     listenerList.remove(listener); 
    } 

    public List<L> getListenerList() { 
     return listenerList; 
    } 
} 
+0

我需要一個文件對話框,也是新的應用程序開發。此代碼是否在活動中?或者我如何使用它? – kingkode