我开始认为我的问题是我的偏好没有正确完成,这就是我无法访问 tem 的原因。这是我的偏好:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="@string/pref_user_profile"
android:textSize="20px"
android:layout="@layout/pref_layout">
<SwitchPreference
android:title="@+string/pref_frequency"
android:summary="@+string/pref_frequency_summary"
android:key="frequency"
android:defaultValue="true"
android:layout="@layout/pref_layout"/>
<SwitchPreference
android:title="@+string/pref_time"
android:summary="@+string/pref_time_summary"
android:key="time"
android:defaultValue="true"
android:layout="@layout/pref_layout"/>
<SwitchPreference
android:title="@+string/pref_symptothermal"
android:summary="@+string/pref_symptothermal_summary"
android:key="symptothermal"
android:defaultValue="true"
android:layout="@layout/pref_layout"/>
<SwitchPreference
android:title="@+string/pref_cervical_mucus"
android:summary="@+string/pref_cervical_mucus_summary"
android:key="cervical_mucus"
android:defaultValue="true"
android:layout="@layout/pref_layout"/>
<SwitchPreference
android:title="@+string/pref_mucus_stamps"
android:summary="@+string/pref_mucus_stamps_summary"
android:key="mucus_stamps"
android:defaultValue="true"
android:layout="@layout/pref_layout"/>
<SwitchPreference
android:title="@+string/pref_fertile_infertile"
android:summary="@+string/pref_fertile_infertile_summary"
android:key="fertile_infertil"
android:defaultValue="true"
android:layout="@layout/pref_layout"/>
</PreferenceCategory>
</PreferenceScreen>
爪哇:
package com.projectcaruso.naturalfamilyplaning;
import com.projectcaruso.naturalfamilyplanning.R;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class UserSettingActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
这是我对设置菜单的调用:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final int RESULT_SETTINGS = 1;
switch (item.getItemId()) {
case R.id.projectcaruso:
Util.goToGitHub(this);
return true;
case R.id.about:
new AlertDialog.Builder(this)
.setTitle(R.string.about)
.setMessage(Html.fromHtml(getString(R.string.about_msg)))
.show();
break;
case R.id.licenses:
new AlertDialog.Builder(this)
.setTitle(R.string.licenses)
.setMessage(Html.fromHtml(getString(R.string.license_detail)))
.show();
break;
case R.id.contact:
break;
case R.id.settings:
Intent j = new Intent(this, UserSettingActivity.class);
startActivityForResult(j, RESULT_SETTINGS);
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.settings, menu);
return true;
}
}
所以任何帮助将不胜感激!我正在尝试访问这些首选项,但不能。似乎可以很好地保存它们。我能够测试和运行代码,更改首选项并保存它们的状态。但是,当我尝试访问它们时,我无法...这是我用来尝试访问它们的代码:
编辑:我已将其更改为如下调用,无论设置如何,它仍然是“Hello toast 2!” @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences 首选项 = this.getActivity().getSharedPreferences("UserSettingActivity",0); // 膨胀这个片段的布局
Boolean symptothermal = preferences.getBoolean("symptothermal", true);
if (!symptothermal) {
Context context = getActivity().getApplicationContext();
Toast.makeText(context, "Hello toast 1!", Toast.LENGTH_LONG).show();
} else {
Context context = getActivity().getApplicationContext();
Toast.makeText(context, "Hello toast 2!", Toast.LENGTH_LONG).show();
}
if (!symptothermal) {
TextView temp = (TextView) getView().findViewById(R.id.temp);
temp.setVisibility(View.GONE);
}
}