2011-10-02 76 views
0

我想創建一個聲音首選項,它與鬧鐘和計時器應用程序中的聲音選擇類似。總之,首選項以標準列表首選項開始,但是一旦選擇了音樂或鈴聲,就會顯示另一個對話框,以便選擇音頻。如何在單個首選項選擇中使用兩個對話框

是我創建自定義首選項處理程序的唯一選擇嗎?我可以捕獲初始列表首選項更改,然後顯示音頻的處理程序嗎?

回答

0

這是我的解決方案。我用我自己的類擴展了ListPreference。當對話框關閉時,我會調用音頻列表的選擇器。當選擇完成時,調用父親PreferenceActivity。

相關的代碼位:

public class AlertBehaviorPreference extends ListPreference { 
... 
    public void onDialogClosed(boolean positiveResult) 
    { 
     super.onDialogClosed(positiveResult); 
     if(positiveResult) 
     { 
      String s = this.getValue(); 
      if(s != null && s.equals(SONG_ALARM_ACTION)) // play song 
      { 
       // Get the parent activity. 
       // This activity will be notified when the audio has been selected 
       PreferenceActivity pActivity = (PreferenceActivity)this.getContext(); 

       // Select a recording 
       Intent i = new Intent(pActivity, pActivity.getClass());   
       i.setAction(Intent.ACTION_GET_CONTENT); 
       i.setType("audio/*"); 
       pActivity.startActivityForResult(Intent.createChooser(i, "Select song"), 1); 

       Log.d(TAG, "Started audio activity chooser"); 
      } 

public class MyPreferenceActivity extends PreferenceActivity { 
... 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // Call back into the preference to save data 
     // Where the result can be validated and settings can be 
     // be reset if the user cancelled the request. 
     ((AlertBehaviorPreference)(findPreference("alertBehaviorPreference"))).onSongActivitySelectionComplete(data); 
    } 
相關問題