作业:制作一个登录界面,以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. S5PV210之添加缺少的-内核提供的'.h'文件 linux3.0.8驱动

    怎样解决编译时出现内核提供的函数或变量没有定义,使用source insight搜索功能找到声明的头文件,然后包含该头件就行了: 比如: error: implicit declaration of ...

  2. [转]AndroidManifest.xml文件详解

    转自:http://www.cnblogs.com/greatverve/archive/2012/05/08/AndroidManifest-xml.html AndroidManifest.xml ...

  3. Git搭建团队开发环境操作演练

    模拟创建远程git仓库 1.首先创建如下目录结构: /Users/hujh/Desktop/GitTest2/GitServer/weibo weibo是我们要创建的项目 2.切换目录 $ cd /U ...

  4. WPF 基础到企业应用系列索引

    转自:http://www.cnblogs.com/zenghongliang/archive/2010/07/09/1774141.html WPF 基础到企业应用系列索引 WPF 基础到企业应用系 ...

  5. hadoop 中对Vlong 和 Vint的压缩方法

    hadoop 中对java的基本类型进行了writeable的封装,并且所有这些writeable都是继承自WritableComparable的,都是可比较的:并且,它们都有对应的get() 和 s ...

  6. 2016 - 1 - 22 HTTP(一)

    一:通过URL找到服务器   1. URL的概念: 1.1 URL全称Uniform Resource Locatior 统一资源定位符 1.2 通过一个URL就可以找到互联网上的唯一的资源. 1.3 ...

  7. IOS开发在线文档 记录下

    View Programming Guide for iOS https://developer.apple.com/library/prerelease/ios/documentation/UIKi ...

  8. iOS interface guidelines (界面设计指南)<一>

    一.      为iOS而设计 1.iOS体现的主题: (1)Deference(顺从):UI的存在就是为了让顾客更加容易理解和进行交互,而不是要和顾客玩智力游戏 (2)Clarity(清晰):在每个 ...

  9. 【LeetCode OJ】Binary Tree Maximum Path Sum

    Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...

  10. PHP extract() 函数

    PHP extract() 函数从数组中把变量导入到当前的符号表中. 对于数组中的每个元素,键名用于变量名,键值用于变量值. 第二个参数 type 用于指定当某个变量已经存在,而数组中又有同名元素时, ...