作业:制作一个登录界面,以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. CSU 1021 B(Contest #3)

    Description 从m个不同元素中取出n (n ≤ m)个元素的所有组合的个数,叫做从m个不同元素中取出n个元素的组合数.组合数的计算公式如下: C(m, n) = m!/((m - n)!n! ...

  2. iOS开发之runtime运行时机制

    最近参加三次面试都有被问到runtime,因为不太懂runtime我就只能支支吾吾的说点零碎.我真的好几次努力想看一看runtime的知识,因为知道理解它对理解OC代码内部变化有一定帮助,不过真心觉得 ...

  3. ASP.NET读取EXCEL文件的三种经典方法(转)

    1.方法一:采用OleDB读取EXCEL文件:  把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下:public DataSet ExcelToDS(string Path) {  str ...

  4. MVC5 烂笔头

    HttpContent Controller:HttpContextBase View:HttpContext.Current View的搜寻顺序:本文件夹.本共享.根共享等 class=" ...

  5. 使用JsonObject解析json

    第一种: [ { "0": "1", "1": "一", "id": "1", ...

  6. 2016 - 1 - 20 runloop学习(2)

    一:CFRunLoopModeRef 1. CFRunLoopModeRef带表RunLoop的运行模式 2. 一个Runloop可以有若干个mode,每个mode又包含若干个sourse,timer ...

  7. char, signed char, and unsigned char in C++

    关于这三者的区别stackoverrflow里有一个答案是这样说的: 3.9.1 Fundamental types [basic.fundamental] 1 Objects declared as ...

  8. 过期邮件替换SQL

  9. UVA821 floyd最短路+暴力

    题意:给n条边,求每两个点之间的平均距离: 思路:数据是100条边,用floyd得到每两点之间的最短距离,然后遍历相加除以边的数目: #include <iostream> #includ ...

  10. IDisposable接口

    C#中IDisposable接口的主要用途是释放非托管资源.当不再使用托管对象时,垃圾回收器会自动释放分配给该对象的内存.但无法预测进行垃圾回收的时间.另外,垃圾回收器对窗口句柄或打开的文件和流等非托 ...