sp存储和读取,保存登录信息练习
2016-05-24 14:51
351 查看
package com.hanqi.testapp111; 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 denglu extends AppCompatActivity { EditText et_1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_denglu); et_1=(EditText)findViewById(R.id.et_1); SharedPreferences sp=getSharedPreferences("a",MODE_PRIVATE); String str =sp.getString("a",null) ; et_1.setText(str); } public void bt_OnClick(View v) { String s1=et_1.getText().toString(); SharedPreferences sharedPreferences=getSharedPreferences("baocun",MODE_APPEND); SharedPreferences.Editor editor=sharedPreferences.edit(); editor.putString("a",s1); editor.commit(); Toast.makeText(denglu.this, "保存数据成功", Toast.LENGTH_SHORT).show(); } }
<?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.testapp111.denglu" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="请输入用户名"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:id="@+id/et_1"/> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" android:onClick="bt_OnClick"/>
![](http://images2015.cnblogs.com/blog/885594/201605/885594-20160524144941366-996607838.jpg)
相关文章推荐
- json:js和jquery中轻量级数据交换格式
- 旅游公司招聘Java工程师
- iOS-OC-UITableViewCell分割线自定义
- C#委托中的协变与逆变
- 实习入职第五天:android View 详解
- tomcat环境下服务器文件句柄耗尽(Too Many Open Files)的问题排查
- [Java并发包学习三]ThreadFactory介绍
- 正则化方法:L1和L2 regularization、数据集扩增、dropout
- json传值以及ajax接收详解
- Android设计模式系列(9)--SDK源码之适配器模式
- 关于css中设定的背景图片无法显示的记录。
- Android 6.0 运行时权限处理完全解析
- PRML第八章:概率图模型
- tomcat中session
- 一个类学习android基础动画类(ObjectAnimator和AnimatorSet和ValueAnimator)
- 面向对象的JavaScript-004
- C++笔记--指针数组和结构
- 【C语言】指向指针的指针变量对字符串进行排序
- python 连接mysql数据库 进行增删改查操作
- android 控件 翻页 ViewPager ViewFlow ViewFlipper