<span style="font-size:18px;">package com.melody.sharedpreference;
import java.io.FileInputStream;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.content.SharedPreferencesCompat;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_name;
private EditText et_passwd;
private CheckBox cb;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
read();
}
//读数据用户名和密码
private void read() {
SharedPreferences sp = getSharedPreferences("login", MODE_PRIVATE);
/*
* arg0: key
* arg1: 表示如果key取不到,回显什么,默认 回显空字符串
*/
String name = sp.getString("name", "");
String passwd = sp.getString("passwd", "");
//回显
et_name.setText(name);
et_passwd.setText(passwd);
}
//初始化控件
private void init() {
et_name = (EditText) findViewById(R.id.et_name);
et_passwd = (EditText) findViewById(R.id.et_passwd);
cb = (CheckBox) findViewById(R.id.cb);
}
//点击登录按钮,保存用户名和密码
public void login(View v){
//获取用户输入的用户名和密码
String name = et_name.getText().toString();
String passwd = et_passwd.getText().toString();
//判断是否选中
if(cb.isChecked()){
//获取sharedpreferences
SharedPreferences sp = getSharedPreferences("login", MODE_PRIVATE);
//获取编辑器
Editor et = sp.edit();
et.putString("name", name);
et.putString("passwd", passwd);
//提交
et.commit();
}
//创建吐司对话框
Toast.makeText(this, "登录成功", 0).show();
}
}
</span>
<span style="font-size:18px;">/布局</span>
<span style="font-size:18px;"><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/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入帐号" />
<EditText
android:id="@+id/et_passwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<span style="white-space:pre"> </span>android:hint="请输入密码" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<span style="white-space:pre"> </span> <CheckBox
<span style="white-space:pre"> </span> android:id="@+id/cb"
<span style="white-space:pre"> </span> android:layout_width="wrap_content"
<span style="white-space:pre"> </span> android:layout_height="wrap_content"
<span style="white-space:pre"> </span> android:text="记住帐号和密码"
<span style="white-space:pre"> </span> android:layout_centerVertical="true"
<span style="white-space:pre"> </span> />
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span> <Button
<span style="white-space:pre"> </span> android:layout_width="wrap_content"
<span style="white-space:pre"> </span> android:layout_height="wrap_content"
<span style="white-space:pre"> </span> android:layout_alignParentRight="true"
<span style="white-space:pre"> </span> android:onClick="login"
<span style="white-space:pre"> </span> android:text="登录"/>
</RelativeLayout>
</LinearLayout>
</span>