Android笔记 SharedPreferences demo
1布局
<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:orientation="vertical"tools:context=".MainActivity" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="请输入用户名" /><EditTextandroid:id="@+id/etusername"android:layout_width="fill_parent"android:layout_height="wrap_content" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="请输入用密码" /><EditTextandroid:id="@+id/etpass"android:layout_width="fill_parent"android:layout_height="wrap_content"android:inputType="textPassword" /><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content" ><CheckBoxandroid:id="@+id/cb_rem_pass"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="记住密码" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:onClick="login"android:text="登陆" /></RelativeLayout></LinearLayout>
2代码
MainActivity.java
package com.example.a24_logform;import com.example.a24_logform.lognservice.LoginService;
import com.example.a28_sharedpreference.R;import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;public class MainActivity extends Activity {private static final String TAG="MainActivity";EditText editname =null;EditText editpass=null;CheckBox checkBox=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editname=(EditText) this.findViewById(R.id.etusername);editpass=(EditText) this.findViewById(R.id.etpass);checkBox=(CheckBox) this.findViewById(R.id.cb_rem_pass);SharedPreferences sp= getSharedPreferences("config", MODE_PRIVATE);String name=sp.getString("name", "");editname.setText(name);String pass=sp.getString("pass", "");editpass.setText(pass);}public void login(View view){String nameString=editname.getText().toString().trim();String passString=editpass.getText().toString().trim();if(TextUtils.isEmpty(nameString)||TextUtils.isEmpty(passString)){Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();}else{if(checkBox.isChecked()){Log.i(TAG, "要保存密码");Toast.makeText(this, "记住密码", Toast.LENGTH_LONG).show();LoginService.saveUserInfo(this,nameString, passString);Toast.makeText(this, "保存成功", Toast.LENGTH_LONG).show();}if("zhangsan".equals(nameString)&&"123".equals(passString)){Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG).show();}else{Toast.makeText(this, "登陆失败,用户名或密码错误", Toast.LENGTH_LONG).show();}}}
}
LoginService.java
package com.example.a24_logform.lognservice;import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;public class LoginService {public static void saveUserInfo(Context context, String name,String pass){SharedPreferences sp=context.getSharedPreferences("config", Context.MODE_PRIVATE);Editor editor= sp.edit();editor.putString("name", name);editor.putString("pass", pass);//采用类似事物的回滚 保证数据同时提交editor.putInt("count", 23);editor.putFloat("PI", 3.1415f);editor.commit();}}
Android笔记 SharedPreferences demo相关推荐
- Android笔记 simpleAdapter demo
1取任意五张图片(不要太大)放到drawable-hdpi文件夹下,我取的系统自带图标 sym_action_add.png sym_action_call.png sym_action_chat.p ...
- Android笔记 简单demo Spinner AutoCompleteTextView Menu
demo1 spinner 1布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android& ...
- Android笔记 对话框demo大全
1布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:too ...
- android中SharedPreferences和PreferenceActivity的存取数据
本文主要介绍SharedPreferences和PreferenceActivity的基础知识和用法. 主要资料来源于网络,包括但不限于: <Android之PreferenceActivity ...
- Android笔记 fragment的生命周期
demo执行顺序 10-22 20:38:10.281: I/System.out(26337): onAttach 附加到Activity 10-22 20:38:10.281: I/Sy ...
- Android组件化demo实现以及遇坑分享
首先贴出demo的github地址:GitHub - TenzLiu/TenzModuleDemo: android组件化demo 前言 前段时间看到最近一直很火的Android组件化然后就自己撸了一 ...
- Android 笔记:识别银行卡,获取银行卡卡号
这个demo是用的card.io 提供的SDK,识别准确率比较高 先看一下效果图: 扫描界面: 识别成功界面 开始界面 在这里说一下集成的两种方式, 1.直接把途中的文件解压开复制到工程目录下的lib ...
- QT 5.4.1 for Android Ubuntu QtWebView Demo
QT 5.4.1 for Android Ubuntu QtWebView Demo 2015-5-15 目录 一.说明: 二.参考文章: 三.QtWebView Demo在哪里? 四.Qt Crea ...
- Android中Sharedpreferences牵涉到跨进程时不能实时读取的问题
转载请注明出处:http://blog.csdn.net/ns_code/article/details/11271053 在做课程表应用时,由于要设置课前提醒的时间,我通过Sharedprefere ...
最新文章
- 4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍
- Django 出现 “multiple primary key defined”报错
- Scala里Unit 与 () Unit的区别
- vue获取编辑器纯文字_前端富文本编辑器 vue-html5-editor
- 许昌往事之压力无处不在
- ArrayList 与 LinkedList 插入、查询效率测试
- sonar不支持mysql_sonar-iOS的实践
- koa2+html模板,Koa2 路由
- SQLyog详细使用教程
- 计算机忘记网络,忘记密码后如何查看电脑无线网络密码
- Microsoft Visual Studio 2005中使用水晶报表详细说明
- 数学中的 argmax 和 argmin 什么意思
- Linux定时器描述符(timerfd)shiy
- Mysql中使用关键字name做字段名
- feign远程调用传参问题
- 删除Karabiner-Elements 和 Karabiner-EventViewer提示项目被锁定
- 服务器重启之后 宝塔面板打不开
- 【ROS-Navigation】—— DWA路径规划算法解析
- 惊闻企业Web应用生成平台 活字格 V4.0 免费了,不单可视化设计器免费,服务器也免费!...
- cookie的path和domain,expired