onResume()方法和ListPreferences Android

时间:2014-04-07 21:55:41

标签: java android onresume listpreference

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


}

}
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>

好吧,如果我选择&#34; Double&#34;例如,我退出此活动回到我的MainActivity,语速仍然相同!我需要从应用程序退出然后它的工作原理!所以这意味着在MainActivity的onResume()中需要调用PreferenceActivity中的值的东西。现在明白了吗?

编辑:

<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>

2 个答案:

答案 0 :(得分:2)

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

}

主要活动

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");
}

答案 1 :(得分:1)

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

我刚刚将下面的内容改为以下内容,我认为这样做更容易。

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