共享首选项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. swift从0加到1000(不包括1000)的五种写法

    用了while, do...while, for in, for in ... { temp += i i++ } println(temp) do { temp2 += j j++ } ) prin ...

  2. 关于全局变量和函数,在其他类中调用问题,extern关键字

    1个工程下有几个类文件,和1个全局的文件comm.h, comm.h中存放了这几个类同时需要的变量和同时调用的函数. 当时,我只在comm.h中定义: int  commData1: vector&l ...

  3. Dictionary通过Value找到它的key

    private void GetDicKeyByValue() { Dictionary<string, string> dic = new Dictionary<string, s ...

  4. There are no accidents.

    愿你攒齐足够多的失望,开启新的生活. 要知道,瀑布是江河走投无路时创造的奇迹

  5. WPF DataGrid

    前台代码 <DataGrid Name="gv_GetWork" AutoGenerateColumns="False" CanUserSortColum ...

  6. 邮件协议POP3/IMAP/SMTP服务的区别

    2016年09月09日 09時51分 wanglinqiang整理 通过网上查找的资料和自己的总结完成了下面的文章,看完之后相信大家对这三种协议会有更深入的理解.如有错误的地方望指正. POP3 PO ...

  7. CTSC模拟题 树上的路径

    Description 给定一棵\(N\)个结点的树,结点用正整数\(1 \dots N\)编号,每条边有一个正整数权值.用\(d(a, b)\)表示从结点\(a\)到结点\(b\)路径上经过边的权值 ...

  8. 在python中使用zookeeper管理你的应用集群

    http://www.zlovezl.cn/articles/40/ 简介: Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些 ...

  9. mysql中TimeStamp和Date的转换

    mysql 查询时间戳(TIMESTAMP)转成常用可读时间格式 from_unixtime()是MySQL里的时间函数 date为需要处理的参数(该参数是Unix 时间戳),可以是字段名,也可以直接 ...

  10. leetcode面试准备:Decode Ways

    1 题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: ...