import java.util.Set;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity {
private EditText edUser, edPw;
private CheckBox chkUser, chkPw;
private Button btLogin, btRegister;
private String isMemory;// isMemory变量用来判断SharedPreferences有没有数据,包括上面的YES和NO
private String isMemorypw;
private final String YES = "yes";
private final String NO = "NO";
private SharedPreferences preferences;// 声明一个SharedPreferences
private final String userInfo = "userinfo";// 用于保存SharedPreferences的文件
private String userName,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edPw = (EditText) findViewById(R.id.edPw);
edUser = (EditText) findViewById(R.id.edUser);
chkPw = (CheckBox) findViewById(R.id.chkPw);
chkUser = (CheckBox) findViewById(R.id.chkUsername);
btLogin = (Button) findViewById(R.id.btLogin);
btRegister = (Button) findViewById(R.id.btRegister);
preferences = getSharedPreferences(userInfo, MODE_PRIVATE);
isMemory = preferences.getString("isMemory", NO);
isMemorypw = preferences.getString("is", NO);
if (isMemory.equals(YES)) {
edUser.setText(preferences.getString("userName", ""));
}
if (isMemorypw.equals(YES)) {
edPw.setText(preferences.getString("password", ""));
}
Editor ed = preferences.edit();
ed.putString("userName", edUser.getText().toString());
ed.putString("password", edPw.getText().toString());
ed.commit();
// 触击登录按钮,执行remenber方法文本框里的信息重新写入SharedPreferences里面覆盖之前的,
// 去除掉勾选框,触击登录按钮执行remenber方法就将之前保存到SharedPreferences的数据清除了
btLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
userName = edUser.getText().toString();
password = edPw.getText().toString();
remember();
}
});
//测试能否读出
btRegister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name = preferences.getString("userName", "");
String habit = preferences.getString("password", "");
Toast.makeText(MainActivity.this, "读取数据如下:"+"\n"+"name:" + name , Toast.LENGTH_SHORT)
.show();
}
});
}
public void remember() {
if (preferences == null) {
preferences = getSharedPreferences(userInfo, MODE_PRIVATE);
}
if (chkUser.isChecked() && chkPw.isChecked()) {
Editor ed = preferences.edit();
ed.putString("userName", edUser.getText().toString());
ed.putString("password", edPw.getText().toString());
ed.putString(isMemory, YES);
ed.putString(isMemorypw, YES);
ed.commit();// 每次使用Editor 向文件中写入数据后,其结尾处一定要使用commit()方法,否则数据不会保存
} else if (chkUser.isChecked()) {
Editor ed = preferences.edit();
ed.putString("userName", edUser.getText().toString());
ed.putString(isMemory, YES);
ed.putString(isMemorypw, NO);
ed.commit();
} else if (chkPw.isChecked() && chkUser.isChecked() == false) {
Toast.makeText(MainActivity.this, "请同时勾选记住账号选项", Toast.LENGTH_SHORT)
.show();
}
}
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sharedpreference_test.MainActivity" >
<LinearLayout
android:id="@+id/topLy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<!-- 登录界面在真个界面的中部 -->
<LinearLayout
android:id="@+id/userLy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<!-- 账号层 -->
<LinearLayout
android:id="@+id/userLy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="账号:" />
<EditText
android:id="@+id/edUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5" />
</LinearLayout>
<!-- 密码层 -->
<LinearLayout
android:id="@+id/pwLy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TextView
android:id="@+id/tvPw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="密码:" />
<EditText
android:id="@+id/edPw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5" />
</LinearLayout>
<!-- 选项层 -->
<RelativeLayout
android:id="@+id/otherLy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<CheckBox
android:id="@+id/chkPw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="48dp"
android:text="记住密码" />
<CheckBox
android:id="@+id/chkUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="35dp"
android:layout_toLeftOf="@+id/chkPw"
android:text="记住账号" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/btLogin"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="登录" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/btRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="注册" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/bottomLy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>