如何在单个首选项选择中使用两个对话框

时间:2011-10-02 19:34:37

标签: android android-preferences

我想创建一个声音首选项,其功能类似于闹钟和计时器应用程序中的声音选择。简而言之,首选项以标准列表首选项开始,但是一旦选择了音乐或铃声,就会显示另一个对话框,允许选择音频。

我是创建自定义偏好处理程序的唯一选择吗?我可以捕获初始列表首选项更改,然后显示音频处理程序吗?

1 个答案:

答案 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);
    }