Android_sharePreference_ex1
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<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="用户名:"
android:textSize="20sp"
/>
<EditText
android:id="@+id/user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
</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="密 码:"
android:textSize="20sp"
/>
<EditText
android:id="@+id/pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
</LinearLayout>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="保存密码"
android:textSize="20sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:onClick="Click"
android:textSize="20sp"/>
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:onClick="Click"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
源代码:
package com.example.sharepreferencesdemo; import java.util.prefs.Preferences; import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
/**
* 利用preferences保存登录的用户名
* @author Administrator
*
*/
public class sharePreferences_example extends Activity{
private EditText user;
private EditText pass;
private Button login;
private Button cancel;
private CheckBox check;
private Editor editor; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pref_examle);
user = (EditText) findViewById(R.id.user);
pass = (EditText) findViewById(R.id.pass);
login = (Button) findViewById(R.id.login);
cancel = (Button) findViewById(R.id.cancel);
check = (CheckBox) findViewById(R.id.check);
SharedPreferences pref = getSharedPreferences("userInfo",MODE_PRIVATE);
editor = pref.edit();
String name = pref.getString("userName", "");
if(name.equals("")){
check.setChecked(false);
}else{
check.setChecked(true);
user.setText(name);
} }
public void Click(View view){
String userName = user.getText().toString().trim();
String passWord = pass.getText().toString();
switch(view.getId()){
case R.id.login:
if("zhangsan".equals(userName)&&"123456".equals(passWord)){
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
if(check.isChecked()){
editor.putString("userName", userName);
editor.commit();
}else{
editor.remove("userName");
editor.commit();
}
}else{
Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
}
break;
case R.id.cancel:
break;
}
}
}
Android_sharePreference_ex1的更多相关文章
随机推荐
- STL sort()函数
C++之所以得到这么多人的喜欢,是因为它既具有面向对象的概念,又保持了C语言高效的特点.STL 排序算法同样需要保持高效.因此,对于不同的需求,STL提供的不同的函数,不同的函数,实现的算法又不尽相同 ...
- hbase分页查询
为了广大技术爱好者学习netty,在这里帮新浪微博@nettying宣传下他出版的新书 <netty权威指南>@nettying兄在华为NIO实践多年,这本书是他的技术和经验的一个结晶.N ...
- Linux协议栈代码阅读笔记(二)网络接口的配置
Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...
- Web 应用的安全性
Web 应用的安全性包括用户认证(Authentication)和用户授权(Authorization)两个部分.用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统.用户授权 ...
- http 需要掌握的知识点(一)
超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.HTTP 也属于 TCP/IP 协议族的子集,想要学习 HTTP ,先需要了解 ...
- 【Stage3D学习笔记续】山寨Starling(七):一般优化方法简介及混合模式
关于事件的优化: 遍历注册的事件由foreach改变为fori,在大量数据遍历时会有提升: 事件池优化: 所有的事件都使用一个对象池进行保存,而不是使用到时进行创建,使用EventDispatcher ...
- [置顶] [BZOJ]2127: happiness 最小割
happiness: Description 高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友.这学期要分文理科了,每个同学对于选择文科与理科有着自己 ...
- PHP+MySQL开发技术详解—学习笔记
1. PHP is Hypertext Preproocessor. 2. Hello World: <?php Echo ‘Hello World!’; ?> 3. ...
- WIN32不得不会:视频播放器
我愿分享我所有的技术,你可愿意做我的朋友? ----赵大哥 为何要写这篇博客 纯WIN32API打造,自认为对底层的理解略有帮助,和大家分享成果和知识点. 已经实现功能有:打开.播放.关闭功能. 核心 ...
- win32进阶之路:给锁屏软件增加一个超链接按钮
前言: windows下一切皆窗口,我们看到的超链接也是窗口,效果图如下: 鼠标放在赵大哥博客园主页和关于软件两个按钮上,按钮上的字体会从绿色变成红色,同时鼠标指针变为手型,点下鼠标左键就会调用默认浏 ...