android 39 共享首选项
共享首选项SharedPreferences:用于存储少量数据,大量数据则存入文件或者sd卡。以键值对保存数据。



activity.java
package com.sxt.day06_05; import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity {
EditText metId,metPwd;
SharedPreferences mSp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
readData();//自动输入密码
}
private void readData() {
mSp=getSharedPreferences("login", MODE_PRIVATE);
String id=mSp.getString("id", "");
String pwd=mSp.getString("pwd", "");
metId.setText(id);
metPwd.setText(pwd);
}
private void setListener() {
findViewById(R.id.btnLogin).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String id=metId.getText().toString();
if(TextUtils.isEmpty(id)){
metId.setError("编号不能为空");
return ;
}
String pwd=metPwd.getText().toString();
if(TextUtils.isEmpty(pwd)){
metPwd.setError("密码不能为空");
return ;
}
mSp=getSharedPreferences("login", MODE_PRIVATE);//文件名为login(data/data/com.sxt.day06_05/shared_prefs/login.xml),MODE_PRIVATE表示私有的
Editor editor = mSp.edit();//Editor是内部接口
editor.putString("id", id);
editor.putString("pwd", pwd);
editor.commit();//存入首选项
Toast.makeText(MainActivity.this, "登陆完成", 2000).show();
}
});
}
private void initView() {
metId=(EditText) findViewById(R.id.etId);
metPwd=(EditText) findViewById(R.id.etPwd);
}
}
页面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
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:id="@+id/etId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="2-10个字符" />
</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:id="@+id/etPwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true"
android:hint="2-10个字符" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/btnLogin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="20dp"
android:text="登陆"/>
<Button
android:id="@+id/btnExit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="退出"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/> </LinearLayout>
</LinearLayout>
login.xml

android 39 共享首选项的更多相关文章
- android学习十三 首选项
1,首选项可用用来持久保存用户设置,游戏最高分等 2,首选项有,列表首选项,复选框首选项,对话框首选项.. 3,通过xml文件和代码创建首选项 addPreferencesFromResou ...
- Android的SharedPreferences(首选项)保存键值对
使用共享首选项 如果您有想要保存的相对较小键值集合,您应使用 SharedPreferences API.SharedPreferences 对象指向包含键值对的文件并提供读写这些文件的简单方法. 每 ...
- 【ubuntu】首选项和应用程序命令(preference & application)
gnome-control-center 系统设置 gnome-control-center region 键盘布局 gnome-control-center screen 屏幕 gno ...
- 我的Android 4 学习系列之文件、保存状态和首选项
目录 使用Shared Preference 保留简单的应用程序数据 保存回话间的Activity实例数据 管理应用程序首选项和创建Preference Screen 保存并加载文件以及管理本地文件系 ...
- mono for android之文件系统与应用程序首选项(转)
Aside from persistent files, your application might need to store cache data in a file. To do that, ...
- 详解Android首选项框架ListPreference
详解Android首选项框架ListPreference 原文地址 探索首选项框架 在深入探讨Android的首选项框架之前,首先构想一个需要使用首选项的场景,然后分析如何实现这一场景.假设你正在编写 ...
- Xamarin android PreferenceActivity 实现应用程序首选项设置(一)
应用程序首选项屏幕 类似系统设置界面. PreferenceActivity 是另一种类型的Activity,通过PreferenceActivity 可以以最少量的工作显示某些Preference列 ...
- Android - Shared Preference (分享首选项) 具体解释
Shared Preference (分享首选项) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24454963 Sh ...
- Android学习笔记(十四)方便实用的首选项-PreferenceActivity
突然发现已经好多天没更新博客了,最近公司项目正在进行一个大跨度的重构,又碰上有新需求,一连好多天都是很晚才到家.其实这篇博文在草稿箱里面也存了很久了,本来想着不发了,不过感觉PreferenceAct ...
随机推荐
- JavaScript学习心得(三)
一 变量 var:变量声明 变量名 =:赋值 简单值类型 全局变量:编程的一般规则——应用程序应该只完成必须的最少功能,如果一个变量不是绝对必需,就不该是全局:全局变量对维护性能不利,因为需要一直维护 ...
- 中国天气网接口返回json格式分析及接口(XML、图片接口)说明
实时天气: city "北京"//城市 cityid "101010100"//城市编码 temp "17&qu ...
- iOS 图片转NSData-b
iOS开发中 UIImage可能经常需要转为NSData 上传 传递等等 有两个比较常用的方法 UIImageJPEGRepresentation UIImagePNGRepresentation 第 ...
- jQuery实现Twitter的自动文字补齐特效
上图效果可以使用jQuery插件Typeahead.js来实现,这款jQuery插件来自于Twitter的一个新的项目,支持远程和本地的数据集.比较有特色的地方在于你可以将数据集使用本地存储(loca ...
- Fibonacci Tree
hdu4786:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:给你一个无向图,然后其中有的边是白色的有的边是黑色的.然后问你是否存在一棵生成树,在 ...
- 【HDU3247】 Resource Archiver(DP+AC自动机+最短路)
Resource Archiver Time Limit: 10000MS Memory Limit: 100000KB 64bit IO Format: %I64d & %I64u ...
- spring没能给struts2创建出action,可能出现的原因
spring没能给struts2创建出action,可能出现的原因: 1.没有加载struts2-spring-plugin-2.1.8.1.jar 2.包冲突,删除asm.jar(网上说的) 3.s ...
- tomcat 系统架构与设计模式 第一部分 系统架构工作原理 转
Tomcat 系统架构与设计模式,第 1 部分: 工作原理 许 令波, Java 开发工程师, 淘宝网 许令波,现就职于淘宝网,是一名 Java 开发工程师.对大型互联网架构设计颇感兴趣,并对一些开源 ...
- linux下USB串口,minicom
[一].驱动相关说明: 如果直接使用串口线,而没有用到USB转串口设备,就不需要安装驱动. 如果使用了USB转串口,一般情况下也不需要安装驱动了,目前linux系统已经包含了该驱动,可以自动识别,亦可 ...
- smtp服务器搭建(实现本地通讯)
1安装postfix 1)下载安装包 sudo apt-get install postfix 2)配置服务器 选择确定. 选择IneternetSite(通过SMTP发送和接收邮件),然后确定 ...