SharedPrefernces使用实例讲解

时间:2021-02-24 15:56:01

activity_main.xml

<LinearLayout 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:orientation="vertical" >
 
<EditText
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Please input your username" />
 
<EditText
android:id="@+id/passWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Please input your password"
android:inputType="textPassword" />
 
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:textSize="30sp" />
 
</LinearLayout>

activity_second.xml

<LinearLayout 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:orientation="vertical" >
 
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
 
<Button
android:id="@+id/btn_showinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="showIfo" />
 
</LinearLayout>

MainActivity.java

package com.example.testsharedpreferencesdemo001;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends Activity
{
 
SharedPreferences sharedPreferences;
Editor editor;
EditText userName, passWord;
Button login;
 
@SuppressLint("CommitPrefEdits")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText) findViewById(R.id.userName);
passWord = (EditText) findViewById(R.id.passWord);
 
login = (Button) findViewById(R.id.btn_login);
login.setOnClickListener(myOnClickListener);
 
sharedPreferences = this.getSharedPreferences("login_info",
MODE_PRIVATE);
 
String stored_username = sharedPreferences.getString("username", "");
String stored_password = sharedPreferences.getString("password", "");
userName.setText(stored_username);
passWord.setText(stored_password);
editor = sharedPreferences.edit();
editor.putString("username", userName.getText().toString());
editor.putString("password", passWord.getText().toString());
 
}
 
OnClickListener myOnClickListener = new OnClickListener()
{
 
@Override
public void onClick(View v)
{
String sUserName = userName.getText().toString();
String sPassWord = passWord.getText().toString();
 
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("username", sUserName);
intent.putExtra("password", sPassWord);
 
startActivity(intent);
 
}
};
}

SecondActivity.java

package com.example.testsharedpreferencesdemo001;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
public class SecondActivity extends Activity
{
String userName;
String passWord;
TextView info;
Button btn_showInfo;
 
@Override
protected void onCreate(Bundle savedInstanceState)
{
 
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_second);
 
info = (TextView) findViewById(R.id.info);
btn_showInfo = (Button) findViewById(R.id.btn_showinfo);
btn_showInfo.setOnClickListener(new OnClickListener()
{
 
@Override
public void onClick(View v)
{
Toast.makeText(SecondActivity.this, userName + ";" + passWord,
Toast.LENGTH_LONG).show();
}
});
Intent intent = this.getIntent();
userName = intent.getStringExtra("username");
passWord = intent.getStringExtra("password");
System.out.println(userName);
System.out.println(passWord);
 
}
 
}

MainActivity

SharedPrefernces使用实例讲解
SharedPrefernces使用实例讲解

点击按钮,跳转到SecondActivty,再次点击按钮showinfo

SharedPrefernces使用实例讲解
SharedPrefernces使用实例讲解

点击“返回键”

回到MainAcitivy
可以看到用户名与密码均保留着
SharedPrefernces使用实例讲解
SharedPrefernces使用实例讲解