简体   繁体   English

使用onResume / onStop作为首选项(设置)

[英]Using onResume/onStop for Preferences (Settings)

started a few days ago with Android, i'm kinda noob. 是几天前从Android开始的,我有点菜鸟。 I got a MainActivity with Weight, Width values. 我有一个MainActivity的Weight,Width值。 These values, can be whether Inches-Pounds or Cm-Kilograms. 这些值可以是英寸-磅或厘米-千克。

How do i change these values? 如何更改这些值? from Settings, i've created a simple Menu with a ListPreference. 从设置,我已经创建了一个带有ListPreference的简单菜单。 When selecting one of those two measures, i need to update my MainActivity with the new values. 当选择这两种措施之一时,我需要使用新值更新我的MainActivity。 Why's that? 为什么? I got the following 我得到以下

TextView : TextEdit TextView:TextEdit

Weight ( Kilograms ) 重量(千克)

[ _ __ _ __ _ ___ ] [ _ __ _ __ _ ___ ]

Width ( Cm ) 宽度(厘米)

[ _ __ _ __ _ ___ ] [ _ __ _ __ _ ___ ]

Those texts between parentheses i want them to be dinamically changed after selecting in the settings menu. 括号内的那些文本我希望在设置菜单中选择后进行动态更改。

This is the part of my code: 这是我的代码的一部分:

MainActivity getting the Preferences from prefs.xml MainActivity从prefs.xml获取首选项

 @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initializeAttributes();

        SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        String measures = getPrefs.getString("MEASURE_KEY", "Inches-Pounds");


        String[] splitMeasure = measures.split("\\-");
        measureWeight.setText(measureWeight.getText() + " ( " + splitMeasure[1] + " )");
        measureHeight.setText(measureHeight.getText() + " ( " + splitMeasure[0] + " )");

This is the prefs.xml with the menu 这是带有菜单的prefs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory                 
        android:title="@string/cat_language">   
        <ListPreference                    
            android:title="@string/title_language"            
            android:summary="@string/sum_language"    
            android:key="LANGUAGE_KEY"                         
            android:entries="@array/languages"            
            android:entryValues="@array/languages" 
            android:defaultValue="English"/>   
    </PreferenceCategory>      

    <PreferenceCategory             
        android:title="@string/cat_usage">   
        <ListPreference                    
            android:title="@string/title_measure"            
            android:summary="@string/sum_measure"    
            android:key="MEASURE_KEY"                    
            android:entries="@array/measures"            
            android:entryValues="@array/measures" 
            android:defaultValue="Inches-Pounds"/>  
    </PreferenceCategory>     

    <PreferenceCategory             
            android:title="@string/cat_help">   
        <Preference               
            android:title="@string/title_howto"  
            android:summary="@string/sum_howto"   
            android:key="HOWTO_KEY" />
        <Preference               
            android:title="@string/title_about"  
            android:summary="@string/sum_about"   
            android:key="ABOUT_KEY" />
    </PreferenceCategory>
</PreferenceScreen>

I even tried using the commit() but couldn't make it work either. 我什至尝试使用commit(),但也无法使其正常工作。

After trying using the onResume() what i got as a result was "fine" except for the fact it was adding me a ( measure ) everytime i change it from the settings, eg: 在尝试使用onResume()之后,我得到的结果是“很好的”,除了事实是,每当我从设置更改它时,它都会为我添加一个(小节),例如:

Weight ( Kilograms ) ( Pounds ) ( Kilograms ) ( Pounds ) and so on. 重量(千克)(磅)(千克)(磅)等。

Can anyone help me on how to use the onResume method in this case? 在这种情况下,有人可以帮助我如何使用onResume方法吗?

Thanks in advance. 提前致谢。

In the ListPreference change android:entryValues to android:entryValues="@array/measuresValues" 在ListPreference中,将android:entryValues更改为android:entryValues =“ @ array / measuresValues”

Your entries arrays should look something like this 您的条目数组应如下所示

<string-array name="measures">
    <item>Inches-Pounds</item>
    <item>Cm-Kilograms</item>
</string-array>   

and your entryValues array 和您的entryValues数组

<string-array name="measuresValues">
    <item>"Inches-Pounds"</item>
    <item>"Cm-Kilograms"</item>
</string-array>  

Then put this in the activity onResume() 然后将其放在活动onResume()中

SharedPreferences mysettings = PreferenceManager.getDefaultSharedPreferences(this);
String st1 = mysettings.getString("MEASURE_KEY", "default Value here");

if(st1.equals("Inches-Pounds")){ 
    //do some stuff here
    //like textview.setText.....
}
if(st1.equals("Cm-Kilograms")){ 
    //do some stuff here
}

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

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