共享参数
SharedPreferences
SharedPreferences
是
Android
的一个轻量级存储工具,采用的存储结构是
Key-Value
的键值对方式,类似
于
Java
的
Properties
类,二者都是把
Key-Value
的键值对保存在配置文件中。
基于
XML
格式的特点,
SharedPreferences
主要适用于如下场合。
(
1
)简单且孤立的数据。若是复杂且相互间有关的数据,则要保存在数据库中。
(
2
)文本形式的数据。若是二进制数据,则要保存在文件中。
(
3
)需要持久化存储的数据。在
App
退出后再次启动时,之前保存的数据仍然有效。
xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:padding="10dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="账号:"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/tv_phone"
android:background="@drawable/editext_selector"
android:gravity="left|center"
android:hint="请输入账号"
android:inputType="text"
android:maxLength="12"
android:textColor="@color/black"
android:textColorHint="@color/grey"
android:textCursorDrawable="@drawable/text_cursor"
android:textSize="17sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="密码:"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/tv_password"
android:background="@drawable/editext_selector"
android:gravity="left|center"
android:hint="请输入密码"
android:inputType="text"
android:maxLength="12"
android:textColor="@color/black"
android:textColorHint="@color/grey"
android:textCursorDrawable="@drawable/text_cursor"
android:textSize="17sp" />
</RelativeLayout>
<CheckBox
android:id="@+id/ck_remember"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@drawable/checkbox_selector"
android:checked="false"
android:padding="10dp"
android:text="记住密码"
android:textColor="@color/black"
android:textSize="10sp" />
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:textColor="@color/black"
android:textSize="20sp" />
</LinearLayout>
activity代码:
public class LoginShareActivity extends AppCompatActivity implements View.OnClickListener {
private SharedPreferences mShared;//声明一个SharedPreferences对象
private EditText et_phone;
private EditText et_password;
private CheckBox ck_remember;
private boolean bRemember = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_share);
et_phone = (EditText) findViewById(R.id.et_phone);
et_password = (EditText) findViewById(R.id.et_password);
findViewById(R.id.btn_save).setOnClickListener(this);
ck_remember = (CheckBox) findViewById(R.id.ck_remember);
ck_remember.setOnCheckedChangeListener(new CheckListener());
//实例化一个SharedPreferences对象
mShared = getSharedPreferences("share_login_save", MODE_PRIVATE);
String phone = mShared.getString("phone", "");
et_phone.setText(phone);
String password = mShared.getString("password", "");
et_password.setText(password);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_save) {
String name = et_phone.getText().toString();
String age = et_password.getText().toString();
if (name == null || name.length() <= 0) {
showToast("请先填写账号");
return;
}
if (age == null || age.length() <= 0) {
showToast("请先填写密码");
return;
}
//如果勾选记住密码,则将账号与密码信息保存在SharedPreferences的share_login_save.xml中,
// 成功登陆后下次再打开页面是自动将保存信息显示出来
if (bRemember) {
SharedPreferences.Editor editor = mShared.edit();
editor.putString("phone", et_phone.getText().toString());
editor.putString("password", et_password.getText().toString());
editor.commit();
showToast("原始数据已经保存");
}
}
}
private void showToast(String desc) {
Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
}
//监听密码记住复选框是否被勾选
private class CheckListener implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == R.id.ck_remember) {
bRemember = isChecked;//被勾选则将bRemember反置
}
}
}
}
1、填写信息1
2、再次打开该页面时保留上次登录信息