SharedPreferences没有保存价值

时间:2013-06-24 14:01:17

标签: android

以下是导致我出现问题的代码:

public class MainActivity extends Activity {

    private TextView lblName;
    private View.OnClickListener droidTapListener;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences prefs = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
        boolean prefsShown = prefs.getBoolean("haveShownPrefs", false);
        setContentView(R.layout.activity_main);


        if (!prefsShown){
            //launch the preferences activity
            Intent i = new Intent(getApplicationContext(), Preferences.class);
            startActivity(i);
        }
        init(prefs);

    }

    private void init(SharedPreferences prefs) {
        lblName = (TextView) findViewById(R.id.lblName);
        lblName.setText(prefs.getString("userName",""));
    }

}

public class Preferences extends Activity {

    private EditText txtName;
    private Button btnDone;
    private View.OnClickListener droidTapListener;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferences);
        SharedPreferences prefs = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = prefs.edit();
        ed.putBoolean("haveShownPrefs", true);
        ed.commit();
        init(prefs, ed);

    }

    private void init(SharedPreferences prefs, SharedPreferences.Editor ed) {

        btnDone = (Button) findViewById(R.id.btnDone);
        txtName = (EditText) findViewById(R.id.txtName);
        ed.putString("userName", txtName.getText().toString());
        ed.commit();
        droidTapListener = new View.OnClickListener()  {
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
            }
        };
        btnDone.setOnClickListener(droidTapListener);
    }

}

如果我遗漏了某些内容,这是XML:

的preferences.xml:     

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context=".Preferences">
    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/name"
                android:id="@+id/lblName"
                android:layout_column="3"/>

        <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textPersonName"
                android:ems="10"
                android:id="@+id/txtName"
                android:layout_column="9"/>

    </TableRow>

    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/done"
                android:id="@+id/btnDone"
                android:layout_column="10"/>
    </TableRow>
</TableLayout>

activity_main.xml中:     

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context=".Preferences">
    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:id="@+id/lblName"
                android:layout_column="5"/>
    </TableRow>
</TableLayout>

问题是,当我在lblName上打印名称时,在MainActivity上它是空白的,我在Prefereces上做了测试:

public void onClick(View v) {
    btnDone.setText(txtName.getText().toString());
}

它工作正常,所以问题在于SharedPreferences。有任何想法吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

Preferences中,init函数读取txtName的内容并将值写入SharedPreferences。您拨打init的唯一地方是onCreate。此时,用户还没有机会在txtName中输入任何测试。在用户输入后,您需要稍后读取其值。根据您的应用程序,您可能希望从按钮侦听器或onPause Preferences方法执行此操作,以便保存该值,但用户选择保留此活动(对于例如,按返回主页)。