1. 程式人生 > >SharedPreferences的使用案例之QQ登入

SharedPreferences的使用案例之QQ登入

package com.example.administrator.case_login; import android.content.Context; 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.CheckBox; import android.widget.EditText; import
android.widget.Toast; public class LoginActivity extends AppCompatActivity{ private EditText etName; private EditText etPassword; private SharedPreferences sharePreferences; private CheckBox cbIsRememberPass; @Override protected void onCreate(Bundle saveInstanceState){ super
.onCreate(saveInstanceState); setContentView(R.layout.activity_login); initViews(); sharePreferences=getSharedPreferences("rememberpassword", Context.MODE_PRIVATE); boolean isRemember=sharePreferences.getBoolean("rememberpassword",false); if(isRemember){ String name=sharePreferences
.getString("name",""); String password=sharePreferences.getString("password",""); etName.setText(name); etPassword.setText(password); cbIsRememberPass.setChecked(true); } } public void initViews(){ etName=(EditText) findViewById(R.id.etName); etPassword=(EditText) findViewById(R.id.etPassword); } public void login(View view){ String name=etName.getText().toString(); String password=etPassword.getText().toString(); if ("admin".equals(name)&&"123456".equals(password)){ SharedPreferences.Editor editor=sharePreferences.edit(); if (cbIsRememberPass.isChecked()){ editor.putBoolean("rememberpassword",true); editor.putString("name",name); editor.putString("password",password); }else{ editor.clear(); } editor.commit(); Intent intent=new Intent(this,MainActivity.class); startActivity(intent); finish(); }else { Toast.makeText(this,"賬號或密碼有誤",Toast.LENGTH_LONG).show(); } } }