RingtonePreference未触发OnPreferenceChangeListener

时间:2012-04-04 13:15:27

标签: android android-widget

我发布的其他所有首选项都会触发其OnPreferenceChangeListener。但是,我的RingtonePreference没有:

p = getPreferenceScreen().findPreference("pref_tone");
String rname = preferences.getString("pref_tone",Settings.System.DEFAULT_RINGTONE_URI.toString());
String name = ringtoneToName(rname);
p.setSummary(name);
p.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
    // Never hits here!
    String v = (String) newValue;
    preference.setSummary(ringtoneToName(v));           
    return true;
}
});

3 个答案:

答案 0 :(得分:11)

onActivityResult必须调用super.onActivityResult才是修复

答案 1 :(得分:2)

请注意,RingtonePreference会为铃声选择器使用“活动”。

如果您在支持PreferenceFragment(android.support.v4.preference.PreferenceFragment)中使用RingtonePreference,则在调用startActivityForResult时,RingtonePreference会错误地使用父Activity而不是PreferenceFragment。这意味着也会在父Activity上调用onActivityResult回调。解决此问题的最简单的解决方法是覆盖父Activity中的onActivityResult,并确保将回调转发到PreferenceFragment。例如:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Fragment f = getSupportFragmentManager().findFragmentByTag(PREFERENCE_FRAGMENT_TAG);
    if (f != null) {
        f.onActivityResult(requestCode, resultCode, data);
    }
}

答案 2 :(得分:0)

那些仍然面临问题的人,如果你正在使用偏好片段,那么在偏好片段中添加以下代码。

<?php
        session_start();
            // define variables and set to empty values
            $nameErr = $emailErr = $phoneErr = $describeErr = $humanErr = $subjectmatterErr = "";
            $Name = $Email = $Phone = $Describe = $Human = 
$Subjectmatter = "";

            $hasError = false;
            $sent = false;

            if(isset($_POST['submit'])) {
                $Name                = trim(htmlspecialchars($_POST['Name'], ENT_QUOTES));
                $Email               = trim($_POST['Email']);
                $Describe            = trim(htmlspecialchars($_POST['Describe'], ENT_QUOTES));
                $Phone               = trim($_POST['Phone']);
                $Company             = trim($_POST['Company']);
                $Website             = trim($_POST['Website']);
                $Subjectmatter       = trim($_POST['Subjectmatter']);
                $Human               = trim($_POST['Human']);
                $Number              = 8;        

                $fieldsArray = array(
                    'Name' => $Name,
                    'Email' => $Email,
                    'Describe' => $Describe,
                    'Phone' => $Phone,
                    'subjectmatter' => $Subjectmatter,
                    'Human' => $Human
                );

                $errorArray = array();

                foreach($fieldsArray as $key => $val) {
                    switch ($key) {
                        case 'Name':
                            if(empty($val)) {
                                $hasError = true;
                                $nameErr = "Please enter your name.";
                            }
                        case 'Name':
                            if (!preg_match("/^[a-zA-Z ]*$/", ($val))) {
                                $hasError = true;
                                $nameErr = "The value must be alphanumeric."; 
                            }
                            break;
                        case 'Phone':
                            if (!preg_match("/^[0-9]+$/", ($val))) {
                                $hasError = true;
                                $phoneErr = "Only numbers and white space allowed.";
                            }
                        case 'Phone':
                            if(empty($val)) {
                                $hasError = true;
                                $phoneErr = "Phone is required.";
                            }
                            break;
                        case 'Subjectmatter':
                            if(empty($val)) {
                                $hasError = true;
                                $subjectmatterErr = "Subject is required.";
                            }
                            break;
                        case 'Email':
                            if(!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
                                $hasError = true;
                                $emailErr = "Email is required.";
                            } else {
                                $Email = filter_var($Email, FILTER_SANITIZE_EMAIL);
                            }
                            break;
                        case 'Describe' :
                            if (strlen($val) < 25) {
                                $hasError = true;
                                $describeErr = "Please enter at least 25 characters. 
    This way I know more specifically what you need!";
                            }
                        case 'Describe' :
                            if(empty($val)) {
                                $hasError = true;
                                $describeErr = "Message is required. 
    This way I know what you need!";
                            }
                            break;
                        case 'Human':
                            if (!preg_match("/[^\d]?8[^\d]?/", ($val))) {
                                $hasError = true;
                                $humanErr = "Not the right answer";
                            }
                        case 'Human':
                            if (!preg_match("/^[0-9]+$/", ($val))) {
                                $hasError = true;
                                $humanErr = "Must be a number";
                            }
                        case 'Human':
                            if(empty($val)) {
                                $hasError = true;
                                $humanErr = "Are you human?";
                            }
                            break;
                    }
                }

                //Validation Success!
                //Do form processing like email, database etc here
                if($hasError !== true) {
                    $priority = $_POST['priority'];
                    $type = $_POST['type'];
                    $message = $_POST['message'];

                    $formcontent=" From: $Name \n Company: $Company \n 
    Email: $Email \n Phone: $Phone \n 
    Website: $Website \n Subject: $Subjectmatter 
    \n Describe: $Describe \n\n";
                    $recipient = "email@email.com";
                    $subject = "Contact Form";
                    $mailheader = "From: $Email \r\n";
                    mail($recipient, $subject, $formcontent, 
$mailheader /*$message, $headers*/);
                    header("Refresh:0; url=thanks.php");
                    exit();       
                }
            }

        ?><!--END PHP-->

此后你的onPreferenceChange方法应该被调用