SharedPreferences自动登录判断
来源:互联网 发布:淘宝店铺怎么投诉 编辑:程序博客网 时间:2024/06/07 05:22
登录页面
package com.bw.sharedpreferences;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
public class LoginActivity extends Activity {
private EditText name_ed, pass_ed;
private CheckBox jz_check, zd_check;
private Button login;
private SharedPreferences preferences;
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); // 1.获取资源ID name_ed = (EditText) findViewById(R.id.name_ed); pass_ed = (EditText) findViewById(R.id.pass_ed); jz_check = (CheckBox) findViewById(R.id.jz_check); zd_check = (CheckBox) findViewById(R.id.zd_check); login = (Button) findViewById(R.id.login); preferences = getSharedPreferences("User", MODE_PRIVATE); // 自动登录 boolean isChecked = preferences.getBoolean("isChecked", false); if (isChecked) { String name = preferences.getString("name", null); String pass = preferences.getString("pass", null); name_ed.setText(name); pass_ed.setText(pass); jz_check.setChecked(true); } // 记住密码 boolean zd_isChecked = preferences.getBoolean("zd_isChecked", false); if (zd_isChecked) { Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } // 两者同时勾选 zd_check.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { jz_check.setChecked(true); } } }); // 记住密码取消勾选 自动登录也取消 jz_check.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked == false) { zd_check.setChecked(false); } } }); // 登录判断 login.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (jz_check.isChecked()) { String name = name_ed.getText().toString();// 用户名 String pass = pass_ed.getText().toString();// 密码 Editor edit = preferences.edit(); edit.putString("name", name); edit.putString("pass", pass); edit.putBoolean("isChecked", true); edit.commit(); } if (zd_check.isChecked()) { Editor edit = preferences.edit(); edit.putBoolean("zd_isChecked", true); edit.commit(); } Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } }); }}
布局
<RelativeLayout 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" ><EditText android:id="@+id/name_ed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="42dp" android:ems="10" android:hint="请输入账号" ></EditText><EditText android:id="@+id/pass_ed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/name_ed" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:ems="10" android:hint="请输入密码" ></EditText><CheckBox android:id="@+id/jz_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/pass_ed" android:layout_centerVertical="true" android:text="记住密码" /><CheckBox android:id="@+id/zd_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/jz_check" android:layout_alignRight="@+id/pass_ed" android:text="自动登录" /><Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="82dp" android:text="登录" /></RelativeLayout>
主界面
package com.bw.sharedpreferences;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {private Button button;private SharedPreferences preferences;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button1); // 点击清空 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { preferences = getSharedPreferences("User", MODE_PRIVATE); Editor edit = preferences.edit(); edit.clear(); edit.commit(); Intent intent = new Intent(MainActivity.this,LoginActivity.class); startActivity(intent); finish(); } }); }}
布局
<RelativeLayout 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" ><TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="登录成功" android:textSize="20sp" /><Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_alignParentBottom="true" android:layout_marginBottom="57dp" android:text="注销" /></RelativeLayout>
- SharedPreferences自动登录判断
- SharedPreferences自动登录系统
- SharedPreferences实现自动登录
- 判断 SharedPreferences 是否是第一次登录
- Android:利用SharedPreferences实现自动登录
- Android:利用SharedPreferences实现自动登录
- Android:利用SharedPreferences实现自动登录
- Android:利用SharedPreferences实现自动登录
- Android:利用SharedPreferences实现自动登录
- Android使用SharedPreferences实现自动登录
- Android自动登录 记住用户名密码实现 SharedPreferences
- Android自动登录 记住用户名密码实现 SharedPreferences
- Android自动登录 记住用户名密码实现 SharedPreferences
- 安卓自动登录demo——SharedPreferences的应用
- SharedPreferences自动登录及存储多组相同类型数据
- 利用SharedPreferences做的自动登录的小Demo
- Android-SharedPreferences实现记住密码和自动登录
- Android自动登录 记住用户名密码实现 SharedPreferences
- springboot 编写启动时监听服务
- Hibernate_映射继承关系
- centos7下mysql5.7忽略表名大小写
- 多线程面试题
- lua面试
- SharedPreferences自动登录判断
- 23种设计模式(5):原型模式
- Mybatis配置文件中<mappers>配置方式
- CountDownLatch同步辅助类简用
- 1132. Cut Integer (20)-PAT甲级真题
- JVM 工作原理和流程
- python编程中的if __name__ == 'main'
- DELETE_FAILED_INTERNAL_ERROR Error while Installing APKs
- 几种远程调用接口协议简单比较