简体   繁体   中英

onResume() method and ListPreferences Android

PreferenceActivity:

    public class UsersettingsActivity extends PreferenceActivity 
{

    public EditTextPreference nomePreferences;
    public static float currValue;
    public static ListPreference lista;
    public static CharSequence currText;
    public static String ilmionome;
    public CheckBoxPreference checkboxPrefSpeech;
    public CheckBoxPreference checkboxPref;
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        this.overridePendingTransition(R.anim.in, R.anim.out);
        /** Customizzo la actionbar */
        ActionBar actionBar = getActionBar();
        actionBar.setCustomView(R.layout.actionbarcustom);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);
    nomePreferences = (EditTextPreference)getPreferenceManager().findPreference("nomepreferenze");

    lista = (ListPreference)getPreferenceManager().findPreference("prefSyncFrequency");

    currText = lista.getEntry();
    currValue = Float.parseFloat(lista.getValue());
    Toast valori = Toast.makeText(getBaseContext(), currText, Toast.LENGTH_SHORT);
    valori.show();

    if(currValue == 2){
        currValue = MainActivity.tts.setSpeechRate(2.0f);
    } else if (currValue == 1) {
        currValue = MainActivity.tts.setSpeechRate(1.0f);
    } else if (currValue == 0.5) {
        currValue = MainActivity.tts.setSpeechRate(0.5f);
    } 


}

}
the arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="syncFrequency">
        <item name="2">Double/item>
        <item name="1">Normal</item>
        <item name="0">half</item>

    </string-array>
    <string-array name="syncFrequencyValues">
        <item name="2">2</item>
        <item name="1">1</item>
        <item name="0">0.5</item>
    </string-array>

</resources>

Well, if i choose "Double" for example, and i exit from this activity to come back to my MainActivity, the speech rate still the same! I need exit from app and then it works! So it means that in the onResume() of MainActivity needs something that calls the values from the PreferenceActivity .. Understand now?

EDIT:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
     <PreferenceCategory
           android:summary="Opzioni lettura sms"
           android:fontFamily="sans-serif-light"
           android:title="Opzioni">

          <CheckBoxPreference
                android:key="firstDependent"
                android:fontFamily="sans-serif-light"
                android:summary="@string/autorizza"
                android:title="@string/letturanotifiche"

          />


         <EditTextPreference
             android:title="@string/nomepreferences"
             android:key="nomepreferenze"
             android:fontFamily="sans-serif-light"
             android:summary="@string/nomepreferencesdesc"
             android:inputType="text"
             android:dialogTitle="@string/nameis"
             />

         <ListPreference
            android:key="prefSyncFrequency"
            android:entries="@array/syncFrequency"
            android:summary="@string/pref_sync_frequency_summary"
            android:entryValues="@array/syncFrequencyValues"
            android:title="@string/pref_sync_frequency" />


    </PreferenceCategory>
<!--Any other categories include here-->
</PreferenceScreen>
public class SomePreference extends PreferenceActivity implements OnSharedPreferenceChangeListener{

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.task_pref);


    SharedPreferences SP = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());


    // Register OnChangeListener
    SP.registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    // TODO Auto-generated method stub
    //Make sure the item changed was the list_preference 
    if(key.equals("")){
        String value = sharedPreferences.getString(key, "Double");

        Intent i = new Intent(this, Main.class);
        startActivity(i);
    }
}

}

Main Activity

public class Main extends Activity{

protected void onCreate(Bundle savedInstanceState){
    \\setup rest here. 

SharedPreferences share_someting = PreferenceManager.getDefaultSharedPreferences(this);
String ss = share_something.getString("prefSyncFrequency", "Double");
}

protected void onResume(){
 \\setup rest here.
SharedPreference share_something = PreferenceManager.getDefaultSharedPrefernce(this);
String ss = share_something("prefSyncFrequency", "Double");
}
protected void onCreate(){
    text_speech();

}
protected void onResume(){
    text_speech();

}

public void text_speech(){


SharedPrefernces spp = PreferenceManager.getDefaultSharedPreferences(this);

String sf = spp.getString("prefSyncFrequency", "Double");
if(sf.equals("two")){
    currValue = MainActivity.tts.setSpeechRate(2.0f);

}else if(sf.equals("single")){
   currValue = MainActivity.tts.setSpeechRate(1.0f);
}else (sf.equals("half")){
    currValue = MainActivity.tts.setSpeechRate(0.5f);
}

I just changed below to follwing, I think its easier work this way.

<string-array name="syncFrequencyValues">
    <item >two</item>
    <item >single</item>
    <item >half</item>
</string-array>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM