2

我想我在从SharedPreferences.

我的代码:

public class SecondActivity extends Activity {

    private TextView password;
    private EditText username;
    private Button button;
    private String name;
    private String pass;
    private Set<String> set;
    private TextView show;
    SharedPreferences myPrefs;
    SharedPreferences.Editor editor;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        myPrefs = this.getPreferences(MODE_PRIVATE);
        editor = myPrefs.edit();

        username = (EditText) findViewById(R.id.editText1);
        password = (EditText) findViewById(R.id.editText2);
        button = (Button) findViewById(R.id.button1);
        show = (TextView) findViewById(R.id.textView3);

        name = username.getText().toString();
        pass = password.getText().toString();

        /*
         * set = new HashSet<String>(); set.add(name); set.add(pass);
         */

    }

    public void saveToPreference(View v) {

        editor.putString("UserName", name);
        editor.putString("Password", pass);
        editor.commit();

        SharedPreferences myPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String use = myPref.getString("UserName", "Sorry");
        String pas = myPref.getString("Password", "SorryAgain");
        Toast.makeText(getApplicationContext(), use, Toast.LENGTH_SHORT).show();
        show.append(use + "\n" + pas);

    }

}

应用程序不会强制/关闭。相反,我得到了 defaultValues;"Sorry","SorryAgain" 检索自SharedPreferences"Sorry"被烤了。

我还应该做什么?

4

2 回答 2

3

您正在修改preferences返回的:

this.getPreferences(MODE_PRIVATE);

然后您正在阅读以下返回的首选项:

PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

您应该初始化 myPrefs ,onCreate然后在所有Activity函数中使用该实例,以确保您使用完全相同的SharedPreferences. 像这样的东西:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

     myPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
     editor = myPrefs.edit();


     username = (EditText)findViewById(R.id.editText1);
     password = (EditText)findViewById(R.id.editText2);
     button = (Button)findViewById(R.id.button1);
     show = (TextView)findViewById(R.id.textView3);

}   


    public void saveToPreference(View v){

    editor.putString("UserName",username.getText().toString());
    editor.putString("Password", password.getText().toString());
    editor.commit();

    String use = myPrefs.getString("UserName", "Sorry");
    String pas = myPrefs.getString("Password", "SorryAgain");
    Toast.makeText(getApplicationContext(), use,Toast.LENGTH_SHORT).show();
    show.append(use+"\n"+ pas);


}
于 2013-07-22T11:16:19.420 回答
1
//In oncreate if its an activity or do it on top of class
    SharedPreferences myPref= getPreferences(MODE_PRIVATE); 
     SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();

//Add data after getting it from edittext
    editor.putString("UserName",name);
     editor.putString("Password", pass);
     editor.commit();

//get data
     String use = myPref.getString("UserName", "Sorry");
     String pas = myPref.getString("Password", "SorryAgain");
于 2013-07-22T11:26:09.777 回答