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的更多相关文章
随机推荐
- POJ 1039 Pipe
题意:一根管子,中间有一些拐点,给出拐点的上坐标,下坐标为上坐标的纵坐标减1,管子不能透过光线也不能折射光线,问光线能射到最远的点的横坐标. 解法:光线射到最远处的时候一定最少经过两个拐点,枚举每两个 ...
- Java多线程 -- 深入理解JMM(Java内存模型) --(五)锁
锁的释放-获取建立的happens before 关系 锁是Java并发编程中最重要的同步机制.锁除了让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息. 下面是锁释放-获取的示例代 ...
- POJ 1661 Help Jimmy
/*96655 's source code for M Memory: 8604 KB Time: 63 MS Language: G++ Result: Accepted */ #include& ...
- 自己手动写http服务器(2)
tringBuilder response =new StringBuilder(); //1) HTTP协议版本.状态代码.描述 response.append("HTTP/1.1&quo ...
- bzoj 2588 Spoj 10628. Count on a tree(主席树)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- HW7.11
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- linux 配置静态IP
ip配置方法是编辑sudo nano /etc/network/interfaces 树莓派默认配置 auto lo iface lo inet loopback iface eth0 inet d ...
- 创建本地CM 离线服务器
一.包管理工具及CentOS的yum 1.包管理工具如何发现可以用的包 包管理工具依赖一系列软件源,工具下载源的信息存储在配置文件中,其位置随某包管理工具不同而变化 使用yum的RedHat/Cent ...
- 你可能没听过的 Java 8 中的 10 个特性
你以前听到的谈论关于Java8的所有都是围绕lambda表达式. 但它仅仅是Java8的一部分. Java 8 有许多新特性—一些强大的新类和语法, 还有其他的从一开始就应该具有的东西. 我将要介绍我 ...
- CentOS安装Redis Sentinel HA集群
安装了很多次,但是每次安装还要翻以前的配置,故列文备忘.下文依赖于2.x版本搭建主从节点实现基于sentinel机制的简单Redis HA(相对高可用Redis集群,真正高可用还要等3.0之后版本). ...