如何在 Android Studio 中首次使用 xml 文件初始化共享首选项值

How to initialize the Shared Preferences values ​using an xml file for the first time in Android Studio

我需要在 Android Studio 中第一次使用 .xml 文件初始化共享首选项值,我只需要第一次读取这些值因为之后这些值会根据用户的需要进行替换。

这是我的 MainActivity:

public class MainActivity extends AppCompatActivity {

private SharedPreferences sharedpreferences;
private static final String mypreference = "mypref";
private EditText mpAddress;
private EditText mPort;
private String mconfig_IpAddress;
private String mconfig_Port;
private Button mInit;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    PreferenceManager.setDefaultValues(this, mypreference, Context.MODE_PRIVATE, R.xml.initial_setup, false);

    mpAddress = (EditText) findViewById(R.id.etx_ipaddress);
    mPort = (EditText) findViewById(R.id.etx_port);
    mInit = (Button) findViewById(R.id.bt_init);

    mInit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            leerConfigWiFiShareref();
            mpAddress.setText(mconfig_IpAddress);
            mPort.setText(mconfig_Port);
        }
    });

}

private void leerConfigWiFiShareref() {
    sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE);

    if (sharedpreferences.contains("SP_ConfigIpAddress")) {
        mconfig_IpAddress = sharedpreferences.getString("SP_ConfigIpAddress", "");
    }
    if (sharedpreferences.contains("SP_ConfigPort")) {
        mconfig_Port = sharedpreferences.getString("SP_ConfigPort", "");
    }
  }
}

还有我的 .xml 文件 initial_setu.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
    android:id="@+id/et1_ip_address"
    android:key="SP_ConfigIpAddress"
    android:defaultValue="192.168.4.1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:digits="0123456789."
    android:inputType="number|numberDecimal"
    android:maxLines="1"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:textColorHighlight="#00ffffff"
    android:title="IP Address" />

<EditTextPreference
    android:id="@+id/et1_port"
    android:key="SP_ConfigPort"
    android:defaultValue="8888"
    android:maxLines="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:digits="0123456789."
    android:inputType="number|numberDecimal"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:textColorHighlight="#00ffffff"
    android:title="Port" />

  </PreferenceScreen>

你已经把readAgain改成了false,这意味着它只读了第一次。

readAgain boolean: Whether to re-read the default values. If false, this method will set the default values only if this method has never been called in the past (or if the KEY_HAS_SET_DEFAULT_VALUES in the default value shared preferences file is false). To attempt to set the default values again bypassing this check, set readAgain to true.

当你调用这个时,这意味着你使用默认的 SharedPreference 你的应用程序。

PreferenceManager.setDefaultValues(this, R.xml.initial_setup, false);
PreferenceManager.getDefaultSharedPreferences(this) // you have to use this

如果您使用自定义名称,您应该试试这个

PreferenceManager.setDefaultValues(this, mypreference, Context.MODE_PRIVATE, R.xml.initial_setup, false);