如何从EditText中的sharedpreferences加载字符串

时间:2014-07-24 19:57:28

标签: android sharedpreferences

我对此很新,并且不想添加这么多代码,但我花了很多时间在这上面我没有选择。我有一个活动/页面,可以节省一些用户详细信息。现在,我正在尝试保存这些详细信息,以便下次打开活动/页面时数据显示出来。

我试过在oncreate方法中启动loadSavedPreferences方法。但是,这不起作用,如果我回到首页并重新进入此活动,那么一切都会消失。代码中的答案将不胜感激。

JAVA:

public class my_settings extends Activity {

EditText name;
EditText email;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_settings);
    name= (EditText) findViewById(R.id.name);
    email= (EditText) findViewById(R.id.email);
    loadSavedPreferences();
}

public void save(View view)
{
    SharedPreferences userDetails = getSharedPreferences("UserDetails", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor=userDetails.edit();
    editor.putString("storedName",name.getText().toString());
    editor.putString("storedEmail",email.getText().toString());
    editor.commit();

    Toast.makeText(this,"Data was saved succesfully",Toast.LENGTH_LONG).show();
}

private void loadSavedPreferences() {
    SharedPreferences userDetails = referenceManager.getDefaultSharedPreferences(this);
    String tempName = userDetails.getString("storedName", "Name");  // (key, default)
    String tempEmail = userDetails.getString("storedEmail","Email Address");
    name.setText(tempName);
    email.setText(tempEmail);

}
}

XML代码如下所示

<RelativeLayout 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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.jesper.im_ok.my_settings"
android:clickable="false">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Please enter contact details here:"
    android:id="@+id/settings_title"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textSize="20sp"
    android:clickable="false"/>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name of Contact"
    android:ems="10"
    android:id="@+id/name"
    android:layout_below="@+id/settings_title"
    android:layout_centerHorizontal="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:text="Email Address"
    android:ems="10"
    android:id="@+id/email"
    android:layout_below="@+id/name"
    android:layout_alignLeft="@+id/name"
    android:layout_alignStart="@+id/name" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

  1. 在save方法中,您使用的是用户创建的getSharedPreferences("UserDetails", Context.MODE_PRIVATE); SharedPreferences表和

  2. 在您访问数据时,尝试从defaultSharedPreferences获取值,这是android提供的默认值。这是错误的。

  3. 在loadSavedPreferences

    中尝试此操作

    SharedPreferences userDetails = this.getSharedPreferences("UserDetails", Context.MODE_PRIVATE);

    希望这会有所帮助!!

答案 1 :(得分:0)

private void loadSavedPreferences() {
    SharedPreferences userDetails = this.getSharedPreferences("UserDetails", Context.MODE_PRIVATE);
    String tempName = userDetails.getString("storedName", "Name");  // (key, default)
    String tempEmail = userDetails.getString("storedEmail","Email Address");
    name.setText(tempName);
    email.setText(tempEmail);
}

测试这个

答案 2 :(得分:0)

要将值放入共享首选项,您需要调用方法编辑“pref.edit()。putString ...”

public class MainActivity extends Activity {

    private SharedPreferences pref               = null;
    private final String      SETTINGS_PREF_NAME = "UserDetails";
    EditText                  email;

    EditText                  name;

    public void save( final View view ) {
        this.pref = this.getSharedPreferences( this.SETTINGS_PREF_NAME, Context.MODE_MULTI_PROCESS );

        this.pref.edit().putString( "storedName", this.name.getText().toString() ).commit();
        this.pref.edit().putString( "storedEmail", this.email.getText().toString() ).commit();

        Toast.makeText( this, "Data was saved succesfully", Toast.LENGTH_LONG ).show();
    }

    @Override
    protected void onCreate( final Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        this.setContentView( R.layout.activity_main );
        this.loadSavedPreferences();
    }

    private void loadSavedPreferences() {
        final String tempName = this.pref.getString( "storedName", "Name" ); // (key, default)
        final String tempEmail = this.pref.getString( "storedEmail", "Email Address" );
        this.name.setText( tempName );
        this.email.setText( tempEmail );

    }
}