1.點擊自動登錄或者記住密碼,點擊登錄跳轉到另一個頁面,另一個頁面的switch的狀態同主界面checkbox的狀態相同。
2.設置一個簡單的登錄頁面,輸入用戶名和密碼,點擊登錄跳轉到另一個頁面,在另一個頁面中點擊保存出現輸入的用戶名和密碼。
第一個主界面佈局及代碼:
佈局:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".SharedPreferencesActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄"
android:id="@+id/save"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="記住密碼"
android:id="@+id/cb2"
android:layout_below="@+id/save" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自動登錄"
android:id="@+id/cb1"
android:layout_below="@+id/save" />
</RelativeLayout>
佈局效果:
代碼:
package com.example.administrator.jreduch09;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class SharedPreferencesActivity extends AppCompatActivity {
private Button save;
private CheckBox cb1;
private CheckBox cb2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save=(Button)findViewById(R.id.save);
cb1= (CheckBox) findViewById(R.id.cb1);
cb2= (CheckBox) findViewById(R.id.cb2);
final SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor =sp.edit();
editor.putBoolean("1",cb1.isChecked());
editor.putBoolean("2",cb2.isChecked());
editor.commit();
Intent intent=new Intent(SharedPreferencesActivity.this,SettingsActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
SharedPreferences sp=getSharedPreferences("switch",MODE_PRIVATE);
boolean bb1=sp.getBoolean("3",false);
boolean bb2=sp.getBoolean("4",false);
cb1.setChecked(bb1);
cb2.setChecked(bb2);
}
}
第一個跳轉界面佈局及代碼:
佈局:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.administrator.jreduch09.SettingsActivity">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自動登錄"
android:id="@+id/switch1"
android:layout_centerHorizontal="true"
android:layout_marginTop="105dp" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="記住密碼"
android:id="@+id/switch2"
android:layout_below="@+id/switch1"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"
android:id="@+id/save"
android:layout_below="@+id/switch2"
android:layout_alignParentStart="true" />
</RelativeLayout>
代碼:
package com.example.administrator.jreduch09;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
public class SettingsActivity extends AppCompatActivity {
private Button save;
private Switch switch1;
private Switch switch2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
save= (Button) findViewById(R.id.save);
switch1= (Switch) findViewById(R.id.switch1);
switch2= (Switch) findViewById(R.id.switch2);
final SharedPreferences sp=getSharedPreferences("switch",MODE_PRIVATE);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor =sp.edit();
editor.putBoolean("3",switch1.isChecked());
editor.putBoolean("4",switch2.isChecked());
editor.commit();
}
});
}
@Override
protected void onStart() {
super.onStart();
SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
boolean b1=sp.getBoolean("1",true);
boolean b2=sp.getBoolean("2", true);
switch1.setChecked(b1);
switch2.setChecked(b2);
}
}
完成效果:第二個主界面佈局及代碼:
佈局:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".SharedPreferencesActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入用戶名"
android:id="@+id/user"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼 "
android:id="@+id/pwd"
android:layout_below="@+id/user"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄"
android:id="@+id/save"
android:layout_below="@+id/pwd"
/>
</RelativeLayout>
代碼:
package com.example.administrator.jreduch09;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class SharedPreferencesActivity extends AppCompatActivity {
private EditText user;
private EditText pwd;
private Button save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user= (EditText) findViewById(R.id.user);
pwd= (EditText) findViewById(R.id.pwd);
save=(Button)findViewById(R.id.save);
final SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor =sp.edit();
editor.putString("userName", user.getText().toString());
editor.putInt("userPwd", Integer.parseInt(pwd.getText().toString()));
editor.commit();
Intent intent=new Intent(SharedPreferencesActivity.this,SettingsActivity.class);
startActivity(intent);
}
});
}
第二個跳轉界面佈局及代碼:佈局:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.administrator.jreduch09.Sp2Activity">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/showUser"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/getUser"
android:text="獲取數據"
android:layout_below="@+id/showUser"
/>
</RelativeLayout>
代碼:package com.example.administrator.jreduch09;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Sp2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp2);
Button getUser = (Button) findViewById(R.id.getUser);
final TextView showUser= (TextView) findViewById(R.id.showUser);
final SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
getUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name= sp.getString("userName","NULL");
String pwd= String.valueOf(sp.getInt("userPwd", 8888));
showUser.setText("用戶名"+name+"\n"+"密碼"+pwd);
}
});
}
}
完成效果: