我在主活動中定義了一些SharedPreferences(稱爲ScoreboardActivity)。值肯定是檢索或正確的默認值正在工作。但是,我現在試圖設置一個SettingsActivity
屏幕,以便用戶可以更改這些值並且它無法正常工作。當新的Activity打開時,值不會加載到XML佈局中的字段中。檢索SharedPreferences在我的設置活動中不起作用
(因爲你將能夠告訴,我是很新,所以請善待)
這裏是與共享偏好(這工作)我ScoreboardActivity代碼:
// get the preferences
prefs = getPreferences(MODE_PRIVATE);
// Load the values or defaults from the SharedPreferences
msMainClockStart = prefs.getLong("Default_Main_Clock", 480000); // 8 minute default
useShotClock = prefs.getBoolean("Use_ShotClock", false);
msShotClockStart = prefs.getLong("Default_Shot_Clock", 24000); // 24 second default
tvPeriodPrefix = prefs.getString("Period_Prefix", getResources().getString(R.string.period));
valMaxPeriods = prefs.getInt("Max_Periods", 4);
Here is my code when the menu button is pressed and Settings is clicked on (I think this is wrong but the settings.xml page does open:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
setContentView(R.layout.settings);
return super.onOptionsItemSelected(item);
}
這裏是我的SettingsActivity:
package com.example.ultimatescoreclock;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.EditText;
public class SettingsActivity extends Activity {
ScoreboardActivity scoreboard = new ScoreboardActivity();
SharedPreferences settings = scoreboard.prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
EditText
strMainMinutes,
strShotSeconds,
strPeriodPrefix,
strMaxPeriods;
CheckBox
cbUseShotClock;
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
// Load the values or defaults from the SharedPreferences
scoreboard.msMainClockStart = settings.getLong("Default_Main_Clock", 480000); // 8 minute default
scoreboard.useShotClock = settings.getBoolean("Use_ShotClock", true);
scoreboard.msShotClockStart = settings.getLong("Default_Shot_Clock", 24000); // 24 second default
scoreboard.tvPeriodPrefix = settings.getString("Period_Prefix", getResources().getString(R.string.period));
scoreboard.valMaxPeriods = settings.getInt("Max_Periods", 4);
strMainMinutes = (EditText) findViewById(R.id.numMainMinutes);
cbUseShotClock = (CheckBox) findViewById(R.id.cbUseShotClock);
strShotSeconds = (EditText) findViewById(R.id.numShotSeconds);
strPeriodPrefix = (EditText) findViewById(R.id.periodPrefix);
strMaxPeriods = (EditText) findViewById(R.id.periodMax);
strMainMinutes.setText(Long.toString(scoreboard.msMainClockStart/1000));
cbUseShotClock.setChecked(scoreboard.useShotClock);
strShotSeconds.setText(Long.toString(scoreboard.msShotClockStart/1000));
strPeriodPrefix.setText(scoreboard.tvPeriodPrefix);
strMaxPeriods.setText(Integer.toString(scoreboard.valMaxPeriods));
}
}
這是我的XML佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/lblMainClock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Main Clock Default (mins)" />
<EditText
android:id="@+id/numMainMinutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number"
android:minEms="4" />
<CheckBox
android:id="@+id/cbUseShotClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|start"
android:text="Use Shot Clock" />
<TextView
android:id="@+id/lblShotClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shot Clock Default (secs)" />
<EditText
android:id="@+id/numShotSeconds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number"
android:minEms="4" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/lblPeriodPrefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Period Prefix (e.g. Q, Shift, etc)" />
<EditText
android:id="@+id/periodPrefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
<TextView
android:id="@+id/lblMaxPeriods"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Maximum Number of Periods" />
<EditText
android:id="@+id/periodMax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minEms="4" />
A(可能)的相關注意事項:你爲什麼使用'new ScoreboardActivity()'實例化一個活動? – Eric
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); //然後你使用 prefs.getBoolean(「keystring」,true);你應該使用上面的行..根據字符串和布爾值。 – itsrajesh4uguys
@Eric - 因爲我很新,想弄明白這一點。我幾乎沒有面向對象的經驗。變量msMainClockStart,useShotClock(etc)都在ScoreboardActivity中定義。沒有實例化ScoreboardActivity,Eclipse沒有識別變量。有什麼更好的方法來做到這一點? –