2013-06-24 83 views
0

这里是造成我的问题代码:SharedPreferences不保存价值

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。有任何想法吗?提前致谢!

+0

您是否曾手动检查过preferences.xml?使用浏览器 – Oli

+0

@Oli我不确定你的意思,如果我用webbrowser打开preferences.xml,那么它只是显示我的xml代码,并且确实检查了它的错误。 我已经将xml添加到问题中,也许我错过了一些东西。 – Aterin

回答

0

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

+0

谢谢你,这的确是问题所在,它令我感到非常愚蠢,也感谢你让我知道onPause! – Aterin

+0

[官方Android开发者培训的这个页面](http://developer.android.com/training/basics/activity-lifecycle/pausing.html)可以帮助你决定是否使用'onPause'。 –