[英]Why am I getting the default value the first time running the app?
我正在按照此处列出的步骤使用设置活动模板,尽管进行了一些更改(例如,现在创建了一个root_preferences.xml
文件,而不是在选择设置活动模板时为不同的首选项屏幕创建多个 XML 文件)我几乎已经确定了下台阶非常相似。
它在最后说:
第一次运行该应用程序时,您应该会在 Toast 中看到“-1”,因为您尚未更改设置。
但是,我看到的不是 -1,而是 US 作为 toast 消息。 我在想那是因为它是默认值,但他们也将默认值设置为美国,但根据说明,他们第一次运行该应用程序时的期望值是 -1 ...怎么会这样?
这是我的代码和我第一次运行该应用程序时得到的结果:
设置活动.java :
package com.example.droidcafe;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
主活动.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =
new Intent(MainActivity.this, OrderActivity.class);
intent.putExtra(EXTRA_MESSAGE, mOrderMessage);
startActivity(intent);
}
});
PreferenceManager.setDefaultValues(this,
R.xml.root_preferences, false);
SharedPreferences sharedPref = PreferenceManager
.getDefaultSharedPreferences(this);
String marketPref = sharedPref
.getString("market", "-1");
displayToast(marketPref);
}
root_preferences.xml
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/pref_header_account">
<ListPreference
app:defaultValue="US"
app:entries="@array/pref_market_titles"
app:entryValues="@array/pref_market_values"
app:key="market"
app:negativeButtonText="@null"
app:positiveButtonText="@null"
app:title="@string/pref_title_account" />
</PreferenceCategory>
</PreferenceScreen>
字符串.xml:
<string name="pref_header_account">Account</string>
<!-- Sync Preferences -->
<string name="pref_title_account">Market</string>
<string-array name="pref_market_titles">
<item>United States</item>
<item>Canada</item>
<item>United Kingdom</item>
<item>India</item>
<item>Japan</item>
<item>Other</item>
</string-array>
<string-array name="pref_market_values">
<item>US</item>
<item>CA</item>
<item>UK</item>
<item>IN</item>
<item>JA0</item>
<item>-1</item>
</string-array>
第一次运行应用程序:
期望值:-1
我知道默认值是美国,但他们的代码实验室默认值也是美国,但代码实验室说它应该第一次显示 -1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.