简体   繁体   English

Android的onResume()方法和ListPreferences

[英]onResume() method and ListPreferences Android

PreferenceActivity: 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 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! 好吧,例如,如果我选择“ Double”,而我退出此活动返回到我的MainActivity,则语速仍然相同! 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? 因此,这意味着在MainActivity的onResume()中需要一些东西来调用PreferenceActivity的值。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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