Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
共享引用。Android,Java_Java_Android_Sharedpreferences - Fatal编程技术网

共享引用。Android,Java

共享引用。Android,Java,java,android,sharedpreferences,Java,Android,Sharedpreferences,很抱歉,我有一个非常新的问题,但我认为这没关系,因为我是Android开发的新手。 我正在学习如何保存我的首选项。我有一个简单的应用程序: package com.foxysoft.prefssimple; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.support.v7.ap

很抱歉,我有一个非常新的问题,但我认为这没关系,因为我是Android开发的新手。 我正在学习如何保存我的首选项。我有一个简单的应用程序:

package com.foxysoft.prefssimple;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    final static String LOG_TAG = "myLogs";

    TextView tvInfo;
    SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvInfo = (TextView) findViewById(R.id.tvInfo);

        sp = PreferenceManager.getDefaultSharedPreferences(this);
    }

    @Override
    protected void onResume() {
        Log.d(LOG_TAG, "onResume");
        Boolean notif = sp.getBoolean("notif", false);
        String address = sp.getString("address", "");
        String text = "Notifications are " + ((notif) ? "enabled, address = " + address : "disabled");
        tvInfo.setText(text);
        super.onResume();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem mi = menu.add(0, 1, 0, "Prefs");
        mi.setIntent(new Intent(this, PrefActivity.class));
        return super.onCreateOptionsMenu(menu);
    }
}
如您所见,我的首选项(PrefActivity.class)有一个小菜单和活动,我的首选项活动有prefs.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference
        android:key="notif"
        android:summary="Enable notifications"
        android:title="Notifications">
    </CheckBoxPreference>
    <EditTextPreference
        android:key="address"
        android:summary="Address for notifications"
        android:title="Address">
    </EditTextPreference>
</PreferenceScreen>
如果我理解正确,它应该在每次调用onResume时都执行我的“notif”复选框false和“address”字符串“”(空)。。或
如果没有-为什么?这两行肯定是在说“如果是假的,地址是空的,就不要”,我说得对吗?

不,你说得不对-因为你正在阅读你的存储的首选项

当没有名为“notif”的首选项时;然后使用您为该方法调用提供的默认值;而false将被返回。但是如果存在保存的首选项“notif”,则将返回为该首选项保存的

只需阅读该方法对应的;它说:

defValue boolean:如果此首选项不存在,则返回的值

返回首选项值(如果存在),或返回defValue

换句话说:如果之前存储了
true
,那么将返回该值


此外,真正的答案是:您没有必要假设
Android API调用是如何工作的-所有这些都有很好的文档记录。因此,如果有疑问,请转向Javadoc。(当然,尽管我们在回答问题时尽量做到准确无误,但您仍然可能会在此处收到错误的答案。真正重要的是API文档所说的内容!)

是的,这些值将为“notif”=false和“address”=空,直到您没有将任何内容保存到这些键为止。因为在检索时,您将默认值传递为notify为false,address为空字符串。
。。。我说得对吗?
否。他们说:“使notif等于保存的首选项值-或将其设置为false(如果找不到)”与字符串相同。如果找不到相应的值,则应用默认值。哈哈,ofc我会的,它说我应该等待5分钟才能接受您的答案!再次感谢你。我想如果我问一个非常详细的问题,我会尽快得到答案:)顺便说一句,你的答案现在被接受了!诀窍在于掌握所有必要的细节;但不是更多;-)
Boolean notif = sp.getBoolean("notif", false);
String address = sp.getString("address", "");