作业:制作一个登录界面,以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存储(作业)的更多相关文章

  1. Android SharedPreferences存储

    原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/Android_SharedPreferences.html 一 概念 SharedPreferen ...

  2. Android SharedPreferences存储详解

    什么是SharedPreferences存储 一种轻量级的数据保存方式 类似于我们常用的ini文件,用来保存应用程序的一些属性设置.较简单的参数设置. 保存现场:保存用户所作的修改或者自定义参数设定, ...

  3. android SharedPreferences 存储对象

    我们知道SharedPreferences只能保存简单类型的数据,例如,String.int等. 如果想用SharedPreferences存取更复杂的数据类型(类.图像等),就需要对这些数据进行编码 ...

  4. Android SharedPreferences存储数据

    SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...

  5. Android -- SharedPreferences存储信息

    背景                                                                                             Share ...

  6. Android SharedPreferences存储map的方法

    在网上查了一些资料后,精简改写后得出自己想用的形式,记录一下 public static void putHashMapData(Context context, String key, Map< ...

  7. Android数据存储方式--SharedPreferences

    Android数据存储方式有如下四种:SharedPreferences.存储到文件.SQLite数据库.内容提供者(Content provider).存储到网络服务器. 本文主要介绍一下Share ...

  8. Android应用开发SharedPreferences存储数据的使用方法

    Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...

  9. Android入门(九)文件存储与SharedPreferences存储

    原文链接:http://www.orlion.ga/578/ Android系统中主要提供了三种方式用于简单地实现数据持久化功能,即文件存储.SharedPreference存储以及数据库存储.当然, ...

随机推荐

  1. 原子操作 Interlocked系列函数

    上一篇<多线程第一次亲密接触 CreateThread与_beginthreadex本质区别>中讲到一个多线程报数功能.为了描述方便和代码简洁起见,我们可以只输出最后的报数结果来观察程序是 ...

  2. DotNetBar v12.7.0.10 Fully Cracked

    更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.7.0.10 如果遇到破解问题可以与 ...

  3. Unity截屏

    方式一:直接使用unity自带的截图函数 Application.CaptureScreenshot(“imagename”); 保存路径: 在PC上保存路径为Application.dataPath ...

  4. MYSQL数据库导入导出(可以跨平台)

    MYSQL数据库导入导出.sql文件 转载地址:http://www.cnblogs.com/cnkenny/archive/2009/04/22/1441297.html 本人总结:直接复制数据库, ...

  5. sourcetree使用问题汇总

    1.可优先查阅博文<git 用户手册 1.5.3及后续版本使用>: 2.问题1 Cloning into 'folder'... warning: templates not found ...

  6. IOS开发中--点击imageView上的Button没有任何反应

    点击imageView上的Button没有任何反应:    解决方法:设置图片的userInteractionEnabled为YES,使该imageView可以与用户进行交互

  7. Struts2 和 spring mvc的 迭代标签常用属性对比

    <s:iterator value="#users" var="u" status="st"> <c:forEach  i ...

  8. 框架设计——MVC IOC

    主要概念: 注:以下概念是自我理解,不是很准确. IOC:Inversion of Control(控制反转). 本来对象创建是通过使用类内部进行创建,现在把对象创建交给container(容器)管理 ...

  9. classPath

    问 spring mvc的web.xml中这个地方的classpath是什么意思? spring springmvc java swnuv 2015年09月25日提问 关注 5 关注 收藏 0 收藏, ...

  10. Linux交叉开发环境搭建 —— 效率之源

    楼主今天终于把所有Linux开发环境需要的软件下载完毕了.虽然以前也是搭建过的,时间久了又折腾了一晚上. 交叉环境: Windows.Linux文件共享 SecureCRT 连接虚拟机终端 工具: V ...