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存储以及数据库存储.当然, ...
随机推荐
- FFT快速傅立叶变换的工作原理
实数DFT,复数DFT,FFTFFT是计算DFT的快速算法,但是它是基于复数的,所以计算实数DFT的时候需要将其转换为复数的格式,下图展示了实数DFT和虚数DFT的情况,实数DFT将时域中N点信号转换 ...
- JNI与NDK简介
最近稍微了解一下JNI和NDK. 网上各种教程给人一种二者不分的感觉, 经过自己运行代码, 将两者的关系理了一下. 就目前了解,JNI应该是java自带的一种调用c和c++等语言(native cod ...
- C语言中输入输出函数
1.1.1 格式化输入输出函数Turbo C2.0 标准库提供了两个控制台格式化输入. 输出函数printf() 和scanf(), 这两个函数可以在标准输入输出设备上以各种不同的格式读写数据.pri ...
- [安卓]softap
http://www.cnblogs.com/javawebsoa/archive/2013/05/29/3106758.html
- SharePoint 2013 开发——工作流架构
博客地址:http://blog.csdn.net/FoxDave SharePoint 2013的工作流较之前有了不同,第一次真正地作为独立的服务的概念推出了.这意味着SharePoint工作流不再 ...
- oracle 之关键字exists
-----------------------------------------------------------------------SQL中EXISTS的用法---------------- ...
- Apache与Nginx的区分比较
什么是Nginx代理代理服务器,它和Apache相比又有什么区别呢?你又该如何选择使用呢,用其中一个还是两者都用?我们将会在这里探索一下这些问题的答案. Apache服务器从1995年就开始使用了.相 ...
- UVA 1639(组合数学)
根据组合数公式C(m,n),由于m可能达到20万,因此转换为ln,之后可以表达为ln(m!)-ln(n!)-ln((m-n)!); 求每一个c[n]时,也要根据杨辉三角求组合数进行转化. 注意long ...
- AngularJS基本指令
<!doctype html> <html ng-app> <head> <meta charset="UTF-8"> & ...
- Android Studio调试功能使用总结
先编译好要调试的程序 1.设置断点 选定要设置断点的代码行,在行号的区域后面单击鼠标左键即可. 2.开启调试会话 点击红色箭头指向的小虫子,开始进入调试. IDE下方出现Debug视图,红色的箭头指向 ...