0

我試圖在首選項中實現文件選取器(我無法找到它)。所以,有Button和TextView。當用戶點擊按鈕時,我需要顯示文件對話框。所以,我必須從我的preferences.xml調用startActivityForResult函數。通過按優先按鈕調用startActivityForResult

一些代碼:

@Override 
protected View onCreateView(ViewGroup parent){ 

    LinearLayout layout = null; 

    try { 
     LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     layout = (LinearLayout)mInflater.inflate(R.layout.file_picker_preference, parent, false); 

     selectFileButton = (Button)layout.findViewById(R.id.file_picker_button); 
     selectFileButton.setOnClickListener(new OnClickListener() {    
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getContext(), FilePickerActivity.class); 
       startActivityForResult(intent, REQUEST_PICK_FILE);//I CANNOT INVOKE THIS!!!!! 
      } 
     }); 
    } 
    catch(Exception e) 
    {   
    } 
    return layout;   
} 

我怎樣才能做到這一點?或者如果有FilePicker的一些實現,它將是最好的答案。

回答

0

使用

Activity activity = (Activity) context; 
activity.startActivityForResult(intent, REQUEST_PICK_FILE); 
+0

可是如何才能讓'Name_of_prefernces_activity'?我應該使用哪個功能? – Nolesh

+0

@Ares你爲onCreateView提供了一個代碼,這個方法在哪個類中?並且該類擴展了哪個類。像活動一樣,首選項? –

+0

它擴展了Preference類。我用上下文來代替。但是沒有'startActivityForResult',只有'startActivity'。 – Nolesh