Android学习10
SharedPreferences
今天练习了利用SharedPreferences保存登录用户名密码;
layout布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"> <EditText
android:id="@+id/et_user"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="15sp"
android:textColor="#FF8F44"
android:background="@drawable/bg_username"
android:hint="用户名"
android:paddingLeft="10dp"
android:paddingRight="10dp"/> <EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="15sp"
android:textColor="#FF8F44"
android:layout_below="@id/et_user"
android:background="@drawable/bg_username"
android:hint="密码"
android:inputType="textPassword"
android:layout_marginTop="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"/> <CheckBox
android:id="@+id/cb_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="记住密码"
android:checked="false"
android:layout_below="@+id/et_password"
android:layout_marginLeft="46dp"
android:layout_marginTop="20dp"/> <CheckBox
android:id="@+id/cb_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自动登录"
android:layout_marginLeft="237dp"
android:layout_marginTop="20dp"
android:layout_below="@+id/et_password"/> <Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/cb_login"
android:layout_marginTop="20dp"
android:background="@drawable/bg_btn4"
android:text="登录"
android:textSize="20sp"
android:textColor="#fff"/> </RelativeLayout>
activity:
package com.example.helloworld.datastorage; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast; import com.example.helloworld.R; public class SpLoginActivity extends AppCompatActivity { private EditText mEtUser;
private EditText mEtPassword;
private Button mBtnlogin;
private CheckBox mCbremember, mCbautomatic;
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mEditor; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp_login); mEtUser = findViewById(R.id.et_user);
mEtPassword = findViewById(R.id.et_password);
mBtnlogin = findViewById(R.id.btn_login);
mCbremember = findViewById(R.id.cb_pwd);
mCbautomatic = findViewById(R.id.cb_login); //获得mSharedPreferences的实例
mSharedPreferences = getSharedPreferences("logindata", MODE_PRIVATE);
//获取一个Edit对象,所有对数据的操作都需要经过Edit
mEditor = mSharedPreferences.edit();
//从SharedPreferences中取出记住密码的状态值
boolean isremember = mSharedPreferences.getBoolean("isremember", false); //判断状态值
if (isremember) {
//取出账号密码
String names = mSharedPreferences.getString("name", null);
String passs = mSharedPreferences.getString("pass", null);
//设置复选框的状态是勾选的状态
mCbremember.setChecked(true);
mEtUser.setText(names);
mEtPassword.setText(passs);
} //取出自动登录的状态值
boolean isautomatic = mSharedPreferences.getBoolean("isautomatic", false);
if (isautomatic) {
//跳转
Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG).show();
} //对登录按钮添加监听
mBtnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//判断记住密码的复选框是否勾选如果勾选就在SharedPreferences中存入账号片密码,状态值
if (mCbremember.isChecked()) {
mEditor.putString("name", mEtUser.getText().toString());//获取EditText里的文本内容
mEditor.putString("password",mEtPassword.getText().toString());
//存入状态值,(代表如果已经勾选了,那么就存一个true,的值)
mEditor.putBoolean("isremember", true);
mEditor.commit();
} //自动登录
if (mCbautomatic.isChecked()) {
//当勾选了自动登录 存一个为true的状态值
mEditor.putBoolean("isautomatic", true);
mEditor.commit();
} //跳转
Toast.makeText(SpLoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show(); }
});
} }

Android学习10的更多相关文章
- android学习10——对顶点着器和片段着色器的理解
图形都是点,线,面组成的.顶点着器指定了顶点的位置,大小和颜色. 看一个顶点着色器的代码 attribute vec4 a_Position; attribute float a_PointSize; ...
- 10、android学习资源整理
1.github上整理好的开源工程 https://github.com/Trinea/android-open-project 2.最流行的android组件大全 http://colobu.com ...
- Android学习系列(10)--App列表之拖拽ListView(上)
研究了很久的拖拽ListView的实现,受益良多,特此与尔共飨. 鉴于这部分内容网上的资料少而简陋,而具体的实现过程或许对大家才有帮助,为了详尽而不失真,我们一步一步分析,分成两篇文章. ...
- 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter
目录(?)[-] 设计Adapter的布局 代码部分 Activity的代码 MyAdapter的代码数据源和构造函数 MyAdapter的代码实现自定义的adapter MyAdapter的代码继续 ...
- Android学习路线总结,绝对干货
title: Android学习路线总结,绝对干货 tags: Android学习路线,Android学习资料,怎么学习android grammar_cjkRuby: true --- 一.前言 不 ...
- Android学习——第一个NDK程序
在前面的学习中,我们已经讲解了关于NDK编程的环境搭建流程,简单的使用我们也通过官网本身自带的例子进行说明了.可是相信大家一定还存在这么的一个疑惑:“如果我要自己利用NDK编写一个Android应用, ...
- Android学习——windows下搭建Cygwin环境
在上一篇博文<Android学习——windows下搭建NDK_r9环境>中,我们详细的讲解了在windows下进行Android NDK开发环境的配置,我们也讲到了在NDk r7以后,我 ...
- Android学习——windows下搭建NDK_r9环境
1. NDK(Native Development Kit) 1.1 NDK简介 Android NDK是一套允许开发人员使用本地代码(如C/C++)进行Android APP功能开发的工具,通过这个 ...
- 【Android学习】《Android开发视频教程》第一季笔记
视频地址: http://study.163.com/course/courseMain.htm?courseId=207001 课时5 Activity基础概念 1.Android开发技术结构 ...
随机推荐
- zabbix的web界面出现乱码解决方案
1.问题描述:当我们搭建好zabbix服务器后,查看监控信息时,发现数据显示的下端文字显示为乱码. 2.解决方式: (1)拷贝windows系统字体: 可根据各自的喜好进行选择,我这边就选择楷体 常规 ...
- 题解 AT4170 【[ABC103A] Task Scheduling Problem】
翻译 有 \(3\) 个正整数 \(a\).\(b\).\(c\),请你输出这 \(3\) 个数中的最大值 \(-\) 最小值的差. 分析 求最大值 \(-\) 最小值的差,我们自然可以使用 for ...
- 怎么解析后台返回数据中\r\n换行
给div添加css样式, white-space: pre-wrap; 即可 文章来源:刘俊涛的博客 欢迎关注公众号.留言.评论,一起学习. _________________________ ...
- K3修改字段名
在K3的BOS中,自定义字段之后我们往往会修改字段名,便于记忆和理解,但是修改字段名之后,只是数据库中的字段名被修改了,BOS中的字段标识并没有被修改,可以通过以下语句将标识和字段名改成一致. sel ...
- sql 应用记录
SELECT * FROM (select aa.*,bb.mentalvisitid, ' then '家庭访视' else '电话' end as BCSFXS ,bb.visitdate, ' ...
- CSS的长度单位
对于css的长度单位真的有必要知道一下.那么css长度单位有哪些呢? 分成两大类: 1.绝对单位:不会因为其他元素的尺寸变化而变化.坚持自我. 2.相对单位:没有一个确定的值,而是由其他元素的尺寸影响 ...
- C语言-字符串典型问题分析
1.典型问题一 下面的程序输出什么为什么? #include <stdio.h> int main() { ] = {}; char src[] = ...
- (转)GC ROOTS
还是英文的技术博客更给力,更清楚,本人懒,没有翻译. In your specific example, in any managed environment, Person is not a GC ...
- 【NOI2002】银河英雄传说
[NOI2002]银河英雄传说 这道题暴力模拟会TLE,因为它是并查集的一个应用…… #include<bits/stdc++.h> using namespace std; ],q[], ...
- 搭建Python开发环境(Mac)
准备 Python官网: https://www.python.org/ Python官方文档: https://docs.python.org/ 环境搭建 简介 pipenv是Python官方推荐的 ...