package com.example.sharedpreferencestest;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private SharedPreferences pref;
private SharedPreferences.Editor editor;
private EditText mEditTextUsername;
private EditText mEditTextPassword;
private Button login;
private CheckBox mRememberPass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 调用PreferenceManager的getDefaultSharedPreferences()方法,
* 这是静态方法,接受一个Context参数,自动使用当前应用程序的包名为前缀命名SharedPreferences文件
*/
pref = PreferenceManager.getDefaultSharedPreferences(this);
mEditTextUsername = (EditText) findViewById(R.id.edit_username);
mEditTextPassword = (EditText) findViewById(R.id.edit_password);
login = (Button) findViewById(R.id.button_login);
mRememberPass = (CheckBox) findViewById(R.id.remember_password);
boolean isRemember = pref.getBoolean("remember_password", false);
String username = pref.getString("username", "");
mEditTextUsername.setText(username);
if(isRemember){
//将账号和密码设置到文本框中
String password = pref.getString("password", "");
mEditTextPassword.setText(password);
mRememberPass.setChecked(true);
}
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String username = mEditTextUsername.getText().toString();
String password = mEditTextPassword.getText().toString();
if(username.equals("Tom")&&password.equals("123456")){
editor = pref.edit();
editor.putString("username", username);
if(mRememberPass.isChecked()){
editor.putBoolean("remember_password", true);
editor.putString("password", password);
}else{
editor.clear();
}
editor.commit();
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
finish();
}
else{
Toast.makeText(MainActivity.this, "登录名或密码错误", Toast.LENGTH_LONG).show();
}
}
});
}
}
布局文件
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sharedpreferencestest.MainActivity" >
<EditText
android:id="@+id/edit_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"/>
<EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:password="true"
android:background="#ffffff"/>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/remember_password"
android:layout_height="wrap_content"/>
<TextView
android:layout_height="wrap_content"
android:text="记住密码"/>
</TableRow>
<Button
android:id="@+id/button_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="登录"
android:background="#3DCC79"/>
</LinearLayout>