Android——SharedPreferences存储(作业)
作业:制作一个登录界面,以SP方式存储用户名。用户下次登录时自动显示上次填写的用户名
layout文件:
<?xml version="1.0" encoding="utf-8"?>
<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: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.hanqi.testapp3.PractiseActivity"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_pt_1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_pt_2"
android:inputType="textPassword"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:onClick="bt_pt_onClick"/>
</LinearLayout>
java类:
package com.hanqi.testapp3; import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class PractiseActivity extends AppCompatActivity {
EditText et_pt_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practise);
et_pt_1 = (EditText)findViewById(R.id.et_pt_1); SharedPreferences sp = getSharedPreferences("abcd",MODE_APPEND);
String str = sp.getString("a",null);
et_pt_1.setText(str);
}
public void bt_pt_onClick(View v)
{
EditText et_pt_1 = (EditText)findViewById(R.id.et_pt_1);
String string1 = et_pt_1.getText().toString();
if(string1 ==null||string1.trim().length()==0)
{
Toast.makeText(PractiseActivity.this, "请正确填写用户名称", Toast.LENGTH_SHORT).show();
return;
}
else
{
SharedPreferences sharedPreferences = getSharedPreferences("abcd", MODE_APPEND);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("a", string1);
editor.commit();
Toast.makeText(PractiseActivity.this, "保存数据成功", Toast.LENGTH_SHORT).show();
}
}
}
效果为:

不输入用户名和密码:

随机输入用户名和密码:


退出应用:

打开应用后出现输入的用户名:

Android——SharedPreferences存储(作业)的更多相关文章
- Android SharedPreferences存储
原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/Android_SharedPreferences.html 一 概念 SharedPreferen ...
- Android SharedPreferences存储详解
什么是SharedPreferences存储 一种轻量级的数据保存方式 类似于我们常用的ini文件,用来保存应用程序的一些属性设置.较简单的参数设置. 保存现场:保存用户所作的修改或者自定义参数设定, ...
- android SharedPreferences 存储对象
我们知道SharedPreferences只能保存简单类型的数据,例如,String.int等. 如果想用SharedPreferences存取更复杂的数据类型(类.图像等),就需要对这些数据进行编码 ...
- Android SharedPreferences存储数据
SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...
- Android -- SharedPreferences存储信息
背景 Share ...
- Android SharedPreferences存储map的方法
在网上查了一些资料后,精简改写后得出自己想用的形式,记录一下 public static void putHashMapData(Context context, String key, Map< ...
- Android数据存储方式--SharedPreferences
Android数据存储方式有如下四种:SharedPreferences.存储到文件.SQLite数据库.内容提供者(Content provider).存储到网络服务器. 本文主要介绍一下Share ...
- Android应用开发SharedPreferences存储数据的使用方法
Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...
- Android入门(九)文件存储与SharedPreferences存储
原文链接:http://www.orlion.ga/578/ Android系统中主要提供了三种方式用于简单地实现数据持久化功能,即文件存储.SharedPreference存储以及数据库存储.当然, ...
随机推荐
- 自定义cursor
cursor: url('绝对路径/big.cur'),auto; //通用方式
- 关押罪犯(noip2010)
解法: (1)搜索(30分) (2)二分(此题属于最大值最小问题) (3)贪心+并查集 下面着重说一下“贪心+并查集” 因为有A.B两座监狱,每个犯人不是在A,就是在B监狱. 至于每个犯人在那个监狱, ...
- CAD系统变量(参数)大全
所谓系统变量就是一些参数,这些参数有些是可以在“选项”或其他对话框中进行设置的,有些这必须通过在命令行输入变量名进行设置,当然对于高手来说,还可以通过二次开发程序来进行控制. CAD有很多的变量,例如 ...
- jQuery easyui 提示框
1:弹出提示窗的使用 (1)屏幕右下弹出提示窗口: $.messager.show({ title:'My Title', msg:'Message will be closed after 4 se ...
- Ubuntu安装Fcitx(小企鹅五笔输入法)
安装配置如下: 1. 安装 fcitx sudo apt-get install fcitx 2. 配置默认输入法为 fcitx im-switch -s fcitx // 注意无须加 sudo 3. ...
- 关于python3 OpenCV的安装和配置
开发环境,win7 32bit, Anaconda3.之前尝试pip安装,但是总是不成功.后来,交流群里面废帝大神让我用conda安装,之后按照conda install --channel http ...
- hdoj 2059 :龟兔赛跑 (DP)[转]
转的别人的找了很多就这个比较好理解. Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下 ...
- 【LeetCode OJ】Sum Root to Leaf Numbers
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...
- DirectX游戏编程入门
刚开始学习D3D,安装完DirectX9后,在VS2008中新建Win32项目· ----------------------------------------------------------- ...
- Quirks模式是什么?
什么是DOCTYPE: DOCTYPE,或者称为 Document Type Declaration(文档类型声明,缩写 DTD).通常情况下,DOCTYPE 位于一个 HTML 文档的最前面的 位置 ...