0

我在我的主要活动(称为记分板活动)中定义了一些 SharedPreferences。这些值肯定会被检索到,或者正确的默认值正在工作。但是,我现在尝试设置一个SettingsActivity屏幕,以便用户可以更改这些值,但它不能正常工作。当新 Activity 打开时,值不会加载到 XML 布局中的字段中。

(如您所知,我对此很陌生,所以请善待)

这是与共享首选项相关的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);
    }

这是我的设置活动:

 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" />

4

2 回答 2

1

您应该首先学习android的基础知识。

你实例化ScoreboardActivity的方式不是android的实例化方式Activity

在您的代码中,我没有找到任何与将数据保存SharedPreferences. 您放在那里的代码仅用于从SharedPreferences两个类中检索数据。

当您使用 UI 组件更改值时,例如更改 CheckBox 状态、更改 EditText 中的文本等,您需要在最终确定值时再次将它们保存到 SharedPreferences(要求用户保存或完成当前活动)。

我建议你阅读一些 android-sdk 的基础知识。

  • 以下是有关保存和检索数据的一些示例和说明SharedPreferences
  • 是一个关于如何开始的android教程Activity。(活动之间切换)
于 2012-12-31T06:41:07.857 回答
0

@Eric & @Adil Soomro - 感谢您的评论。你让我走上了正确的轨道,我发现了如何在我的 SettingsActivity 中完成对 SharedPreferences 的检索。

这是我为将来可能感兴趣的任何人更改的代码...

检索初始值的主要活动代码:

    // get the preferences
    prefs = getPreferences(MODE_PRIVATE);

    // Load the values or defaults from the SharedPreferences
    msMainClockStart = prefs.getLong( "Default_Main_Clock", DEFAULT_MAIN_CLOCK_START);
    useShotClock = prefs.getBoolean( "Use_ShotClock", DEFAULT_USE_SHOT_CLOCK );
    msShotClockStart = prefs.getLong( "Default_Shot_Clock", DEFAULT_SHOT_CLOCK_START);
    tvPeriodPrefix = prefs.getString( "Period_Prefix", DEFAULT_PERIOD_PREFIX );
    valMaxPeriods = prefs.getInt( "Max_Periods", DEFAULT_PERIOD_MAX );

onOptionsItemSelected 方法:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent = new Intent(this, SettingsActivity.class);       
    startActivity(intent);

    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 {

EditText
    strMainMinutes,
    strShotSeconds,
    strPeriodPrefix,
    strMaxPeriods;

long
    msMainClockStart,
    msShotClockStart;

int valMaxPeriods;

CheckBox cbUseShotClock;

SharedPreferences prefs;

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

    // get the Shared Preferences
    prefs  = getPreferences(MODE_PRIVATE);

    // assign the views
    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);

    // Load the values or defaults from the SharedPreferences
    msMainClockStart = prefs.getLong( "Default_Main_Clock", DEFAULT_MAIN_CLOCK_START);
    cbUseShotClock.setChecked( prefs.getBoolean( "Use_ShotClock", DEFAULT_USE_SHOT_CLOCK) );
    msShotClockStart = prefs.getLong( "Default_Shot_Clock", DEFAULT_SHOT_CLOCK_START);
    strPeriodPrefix.setText( prefs.getString("Period_Prefix", DEFAULT_PERIOD_PREFIX) );
    valMaxPeriods = prefs.getInt( "Max_Periods", DEFAULT_PERIOD_MAX );

    // convert the values that need converting
    strMainMinutes.setText( Long.toString(msMainClockStart / 60000) );
    strShotSeconds.setText( Long.toString(msShotClockStart / 1000) );
    strMaxPeriods.setText( Integer.toString(valMaxPeriods) );

}

}
于 2012-12-31T22:24:17.767 回答