共享首选项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 共享首选项的更多相关文章

  1. android学习十三 首选项

    1,首选项可用用来持久保存用户设置,游戏最高分等 2,首选项有,列表首选项,复选框首选项,对话框首选项.. 3,通过xml文件和代码创建首选项      addPreferencesFromResou ...

  2. Android的SharedPreferences(首选项)保存键值对

    使用共享首选项 如果您有想要保存的相对较小键值集合,您应使用 SharedPreferences API.SharedPreferences 对象指向包含键值对的文件并提供读写这些文件的简单方法. 每 ...

  3. 【ubuntu】首选项和应用程序命令(preference & application)

     gnome-control-center  系统设置  gnome-control-center region  键盘布局  gnome-control-center screen  屏幕  gno ...

  4. 我的Android 4 学习系列之文件、保存状态和首选项

    目录 使用Shared Preference 保留简单的应用程序数据 保存回话间的Activity实例数据 管理应用程序首选项和创建Preference Screen 保存并加载文件以及管理本地文件系 ...

  5. mono for android之文件系统与应用程序首选项(转)

    Aside from persistent files, your application might need to store cache data in a file. To do that, ...

  6. 详解Android首选项框架ListPreference

    详解Android首选项框架ListPreference 原文地址 探索首选项框架 在深入探讨Android的首选项框架之前,首先构想一个需要使用首选项的场景,然后分析如何实现这一场景.假设你正在编写 ...

  7. Xamarin android PreferenceActivity 实现应用程序首选项设置(一)

    应用程序首选项屏幕 类似系统设置界面. PreferenceActivity 是另一种类型的Activity,通过PreferenceActivity 可以以最少量的工作显示某些Preference列 ...

  8. Android - Shared Preference (分享首选项) 具体解释

    Shared Preference (分享首选项) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24454963 Sh ...

  9. Android学习笔记(十四)方便实用的首选项-PreferenceActivity

    突然发现已经好多天没更新博客了,最近公司项目正在进行一个大跨度的重构,又碰上有新需求,一连好多天都是很晚才到家.其实这篇博文在草稿箱里面也存了很久了,本来想着不发了,不过感觉PreferenceAct ...

随机推荐

  1. Android2.2 API —— ImageView

    注意 请查看本文后期更新完整版: http://www.cnblogs.com/over140/archive/2011/06/08/2075054.html 来源: 农民伯伯: http://www ...

  2. Java DecimalFormat数据格式化例子

    public static void main (String args[]) { DecimalFormat dFormat = new DecimalFormat(".##") ...

  3. uboot的mtd功能支持

    一.概述 1.MTD MTD是Flash的一种管理方法,将Flash划分成几个分区,便于管理. u-boot的MTD功能是在文件cmd_jffs2.c中实现的,由此我们可以知道怎样打开u-boot的M ...

  4. 《深入剖析Tomcat》阅读(二)

    Tomcat是基于Sun公司标准的开源Servlet容器. Servlet是什么? Servlet(Server Applet),全称Java Servlet,未有中文译文.是用Java编写的服务器端 ...

  5. asp.net学习

    http://www.cnblogs.com/fish-li/archive/2011/12/27/2304063.html

  6. OGNL-action

    需要注意的是,action需要先被调用到,OGNL才能成功,因为action被执行才被压入值栈 package com.wolfgang.action; import com.opensymphony ...

  7. bzoj 2594: [Wc2006]水管局长数据加强版 动态树

    2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 934  Solved: 291[Submit][Sta ...

  8. [BZOJ 3198] [Sdoi2013] spring 【容斥 + Hash】

    题目链接:BZOJ - 3198 题目分析 题目要求求出有多少对泉有恰好 k 个值相等. 我们用容斥来做. 枚举 2^6 种状态,某一位是 1 表示这一位相同,那么假设 1 的个数为 x . 答案就是 ...

  9. iPhone开发 - 常用库

    iPhone开发 - 常用库 这里总结了iPhone开发者开发过程中可能需要的一些资源 如何用Facebook graphic api上传视频: http://developers.facebook. ...

  10. Servlet 中使用POI生成Excel

    使用的是poi3.13 http://mvnrepository.com/artifact/org.apache.poi/poi/3.13 import java.io.IOException; im ...