sharedperferences 记住密码,自动登录demo
来源:互联网 发布:小程序官方demo源码 编辑:程序博客网 时间:2024/06/08 13:52
xml 文件
<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"
tools:context=".MainActivity"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:id="@+id/lin1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="7"
android:id="@+id/name"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:id="@+id/lin2"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="7"
android:id="@+id/pass"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:id="@+id/lin3"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<CheckBox
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录" />
</LinearLayout>
<Button
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="登录"
/>
</LinearLayout>
代码
package com.example.sharedperferences_demo1;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
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 Activity {
private EditText name,pass;
private SharedPreferences sp;
private Button btn;
private CheckBox check,check1;
private Editor edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//组件
name=(EditText) findViewById(R.id.name);
pass=(EditText) findViewById(R.id.pass);
check=(CheckBox) findViewById(R.id.check);
check1=(CheckBox) findViewById(R.id.check1);
btn=(Button) findViewById(R.id.btn);
//sharedperferences
sp=getSharedPreferences("user", MODE_PRIVATE);
edit = sp.edit();
//拿到状态码并判断
boolean zzd = sp.getBoolean("jz", false);
boolean zdd = sp.getBoolean("zd", false);
if(zzd){
String strname = sp.getString("name", null);
String strpass = sp.getString("pass", null);
name.setText(strname);
pass.setText(strpass);
check.setChecked(true);
}
if(zdd){
check1.setChecked(true);
Intent it=new Intent(MainActivity.this,LoginActivity.class);
startActivity(it);
finish();
}
//按钮监听
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//判断记住密码选框
if (check.isChecked()) {
String namestr = name.getText().toString();
String passstr = pass.getText().toString();
edit.putString("name", namestr);
edit.putString("pass", passstr);
//状态码
edit.putBoolean("jz", true);
edit.commit();
}
//判断自动登录选框
if(check1.isChecked()){
edit.putBoolean("zd", true);
edit.commit();
}
Intent it=new Intent(MainActivity.this,LoginActivity.class);
startActivity(it);
}
});
//清除数据
Editor ed = sp.edit();
ed.clear();
ed.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
- sharedperferences 记住密码,自动登录demo
- sharedperferences 记住密码,自动登录demo
- SharedPerferences 记住密码
- 记住密码,自动登录
- 记住密码和自动登录
- php 记住密码自动登录
- php记住密码自动登录
- php 记住密码自动登录
- 自动登录和记住密码
- 记住密码和自动登录
- Android记住密码自动登录
- 记住密码和自动登录
- iOS 记住密码\自动登录
- java实现密码自动登录,记住密码
- 记住密码、自动登录、显示密码
- java实现密码自动登录,记住密码
- iOS 自动登录或者记住密码,求个Demo,不知如何调用
- php实现记住密码自动登录方法
- TensorFlow-gpu 在 CentOS 7 下的完全安装手册
- codeforces 900D Unusual Sequences (数论)
- 课后作业之Email邮箱地址注册
- Oracle执行计划详解
- springboot 相关学习
- sharedperferences 记住密码,自动登录demo
- 电商底部按钮 Fragmentlayout和事务
- 点击按钮弹出框
- 关于Apache HTTPD 2.2.15的部分漏洞修复建议
- 一些·angular简单验证方式
- 阿里云---阿里云服务器ECS安装jdk环境
- Java WebService 接口的简单生成和调用
- openSSL将.crt证书生成.bks
- $.each遍历json对象