我如何获得preferences在Android的工作吗? [英] How do I get preferences to work in Android?

查看:208
本文介绍了我如何获得preferences在Android的工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的一直在努力通过这一点。新来的Java / Android系统。我在写我的第一个应用程序,这是一个已经采取了我长于搜索找出了几天的第一件事。这里的设置:这是一个BAC计算器/饮料柜台:

I've really been struggling through this. New to Java/Android. I'm writing my first app and this is the first thing that has taken me longer than a couple days of searching to figure out. Here's the setup: It's a BAC calculator / drink counter:

一个公式来计算BAC。这里的forumla:

A formula is used to calculate the BAC. Here's the forumla:

Bac = ((StandardDrinks / 2) * (GenderConstant / Weight)) - (0.017 * Hours);

因此​​,大家可以看到,能够改变性别和体重会产生更准确和个性化的结果。所以,我有他们的双打:

So as you can see, being able to modify the gender and weight will produce more accurate and personalized results. So I have them as doubles:

double GenderConstant = 7.5; //9 for female
double Weight = 180;

要更改这些变量,我想这个人能够进入的设置,选择不同的值。我有这些东西成立,但没有链接到上面,因为我不能为我的生活弄清楚如何显示的变量。在这里,他们是:

To change these variables I would like the person to be able to go into the settings and choose different values. I have these things set up, but not linked to the variables shown above because I cannot for the life of me figure out how. Here they are:

我preSS菜单按钮,此弹出。大。我将点击设置。

I press the menu button and this pops up. Great. I'll click Settings.

现在的preferences弹出。这是我的preferences.xml:

Now the preferences pops up. Here is my preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

 <PreferenceCategory android:title="Personal Settings">


 <ListPreference
 android:title="Gender"
 android:summary="Verify or deny the presence of a Y chromosome."
 android:key="genderPref"
 android:defaultValue="male"
 android:entries="@array/genderArray"
 android:entryValues="@array/genderValues" />

 <ListPreference
 android:title="Weight"
 android:summary="How much the planet pulls on you, in pounds."
 android:key="weightPref"
 android:defaultValue="180"
 android:entries="@array/weightArray"
 android:entryValues="@array/weightValues" />

 </PreferenceCategory>

<PreferenceCategory android:title="Drink Settings">

 <ListPreference
 android:title="Beer Size"
 android:summary="The volume of your beer, in ounces."
 android:key="beerPref"
 android:defaultValue="12"
 android:entries="@array/beerArray"
 android:entryValues="@array/beerValues" />

 <ListPreference
 android:title="Shot Size"
 android:summary="The volume of your shot, in ounces."
 android:key="shotPref"
 android:defaultValue="1.5"
 android:entries="@array/shotArray"
 android:entryValues="@array/shotValues" />

 <ListPreference
 android:title="Wine Size"
 android:summary="The volume of your wine, in ounces."
 android:key="winePref"
 android:defaultValue="5"
 android:entries="@array/wineArray"
 android:entryValues="@array/wineValues" />


 </PreferenceCategory>
 </PreferenceScreen>

前进体重列表preference:

Onward to the weight ListPreference:

这显示了。该值存储为字符串数组的RES /价值/ arrays.xml。这里有一个样品只需重量的,:

And that shows up. The values are stored as string-arrays in res/values/arrays.xml. Here's a sample, of just the weight ones:

<string-array name="weightArray">
<item>120 lbs</item>
<item>150 lbs</item>
<item>180 lbs</item>
<item>210 lbs</item>
<item>240 lbs</item>
<item>270 lbs</item>
 </string-array>
 <string-array name="weightValues">
<item>120</item>
<item>150</item>
<item>180</item>
<item>210</item>
<item>240</item>
<item>270</item>
 </string-array>

这基本上是据我已经得到了。我可以点击一个值,肯定的,但它不会改变的公式,因为它不符合我的DrinkingBuddy.java创建的双打链接。所有的设置显示的东西都只是空壳,现在,包括主要布局的微调(默认时间只是设置为1小时)

This is basically as far as I've gotten. I can click a value, sure, but it doesn't change the formula because it's not linked with the doubles I created in DrinkingBuddy.java. All of the stuff displayed in the settings are just empty shells for now, including the spinner on the main layout (the default time is just set to 1 hour)

我没有创造preferences.java并试图实施code在各地的网络教程和资源发现的各种组合,但无济于事。在这里,它是无论如何,充满了失败的尝试,使啤酒preF(设置选项来改变多少盎司的啤酒)与可变在我的主类关联:

I did create a Preferences.java and have tried implementing various combinations of code found in tutorials and resources around the web, but to no avail. Here it is anyway, filled with failed attempts to make beerPref (the settings option to change how many ounces in the beer) correlate with a variable in my main class:

package com.dantoth.drinkingbuddy;


 import android.app.Activity;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.preference.Preference;
 import android.preference.PreferenceActivity;
 import android.preference.Preference.OnPreferenceClickListener;


 public class Preferences extends PreferenceActivity {

public static final String PREF_BEER_SIZE = "PREF_BEER_SIZE";

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 addPreferencesFromResource(R.xml.preferences);

 //Get the custom preference



 Preference beerPref = (Preference) findPreference("beerPref");
 beerPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

 public boolean onPreferenceClick(Preference preference) {

 SharedPreferences customSharedPreference = getSharedPreferences("myCustomSharedPrefs", Activity.MODE_PRIVATE);
 SharedPreferences.Editor editor = customSharedPreference.edit();

 editor.commit();
 return true;
 }}

 );}
 }

一个完整的教程和样本code,因为我还没有找到任何可靠的导游在那里将是真棒。

A full on tutorial and sample code would be AWESOME as I've yet to find any reliable guides out there.

推荐答案

我还在这一切了自己,但(有些来自我的版本改编)我觉得你的preferences类只需要做到以下几点:

I'm still working all this out myself, but (somewhat adapted from my version) I think your Preferences class only needs to do the following:

public class Preferences extends PreferenceActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // load the XML preferences file
        addPreferencesFromResource(R.xml.preferences);
    }
}

然后在你的主类,你可以参考preferences:

Then in your main class, you can refer to the preferences:

public class DrinkingBuddy extends Activity 
                           implements OnSharedPreferenceChangeListener {

    private int weight;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

        // register preference change listener
        prefs.registerOnSharedPreferenceChangeListener(this);

        // and set remembered preferences
        weight = Integer.parseInt((prefs.getString("weightPref", "120")));
        // etc
    }

    // handle updates to preferences
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        if (key.equals("weightValues")) {
            weight = Integer.parseInt((prefs.getString("weightPref", "120")));
        }
        // etc
    }
}

的preference更新的储蓄为您处理。

The saving of preference updates is handled for you.

(不太确定公/私声明!)

(Not too sure about public/private declarations!)

这篇关于我如何获得preferences在Android的工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆