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 ...
随机推荐
- python包管理器pip
步骤一:下载pip包 https://pypi.python.org/pypi/pip 步骤二:安装pip包 解压后,到pip包目录执行: python setup.py install 步骤三:添加 ...
- chmod 命令
来源网址: http://www.2cto.com/os/201205/130236.html http://www.cnblogs.com/younes/archive/2009/11/20/160 ...
- Ubuntu 误改/etc/sudoers 文件权限
添加用户时不小心修改了/etc/sudoers 权限,结果不能sudo了,Ubuntu默认关闭root帐户,结果傻X了,无法改回了. 方法如下: 1.重启机器,开机按ESC,进入恢复模式 2.此时,磁 ...
- 简单python2.7.3安装setuptools模块
下载setuptools https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg 安装 .6c11- ...
- Colletion View 简单的备忘
UIColletionView 这篇只是做UIColletionView的常用属性.代理方法和数据源方法的备忘,之后做一些自定义布局,增加删除动画等. UIColletionViewFlowLayou ...
- 调用相册怎么设置剪裁-b
//创建一个相册控制器 UIImagePickerController *pc = [[UIImagePickerController alloc] init]; //图片来源// UIImagePi ...
- 静态代理VS动态代理
代理Proxy: Proxy代理模式是一种结构型设计模式,主要解决的问题是:在直接访问对象时带来的问题 代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问.代理类负责为委 ...
- [BZOJ 1086] [SCOI2005] 王室联邦 【树分块】
题目链接:BZOJ - 1086 题目分析 这道题要求给树分块,使得每一块的大小在 [B, 3B] 之间,并且可以通过一个块外的节点(块根)使得整个块联通. 那么我们使用一种 DFS,维护一个栈,DF ...
- Hadoop下各技术应用场景
数据采集和DataFlow 对于数据采集主要分为三类,即结构化数据库采集,日志和文件采集,网页采集.对于结构化数据库,采用Sqoop是合适的,可以实现结构化数据库中数据并行批量入库到hdfs存储.对于 ...
- Unity 截取图片并且显示出来
Unity 截取图片并且显示出来 近期用到了unity的截图,并且把他显示出来,摸索了很多! 讲解一个东西,android可以存储一些文件在本地,比如配置文件等! 对于不同的系统,方法不一! if ( ...