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的更多相关文章

随机推荐

  1. log4j和web.xml配置webAppRootKey 的问题

    在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为“webapp.root”,如下: <!-- 应用路径 --> &l ...

  2. 《深入Java虚拟机学习笔记》- 第7章 类型的生命周期/对象在JVM中的生命周期

    一.类型生命周期的开始 如图所示 初始化时机 所有Java虚拟机实现必须在每个类或接口首次主动使用时初始化: 以下几种情形符合主动使用的要求: 当创建某个类的新实例时(或者通过在字节码中执行new指令 ...

  3. A Fast Priority Queue Implementation of the Dijkstra Shortest Path Algorithm

    http://www.codeproject.com/Articles/24816/A-Fast-Priority-Queue-Implementation-of-the-Dijkst http:// ...

  4. POJ2104 K-th Number 划分树 模板题啊

    /*Source Code Problem: 2104 User: 96655 Memory: 14808K Time: 1282MS Language: G++ Result: Accepted S ...

  5. NOIP2011 计算系数

    1计算系数 给定一个多项式 (ax + by)k ,请求出多项式展开后 x n y m 项的系数. [输入] 输入文件名为 factor.in. 共一行,包含 5 个整数,分别为 a,b,k,n,m, ...

  6. leetcode@ [129] Sum Root to Leaf Numbers (DFS)

    https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...

  7. Unity3D入门之JavaScript动态创建对象

    接着上一篇Unity3D入门文章,这里继续使用JavaScript脚本语言. 调试:Unity集成了MonoDevelop编辑器,在代码某行的左侧点击,即可下一个断点.然后先关闭Unity编辑器,在M ...

  8. php透明合并png与jpg图片

    源 <?php $png = imagecreatefrompng('./mark.png'); $jpeg = imagecreatefromjpeg('./image.jpg'); list ...

  9. 转载最佳JQuery学习网站

    转载文章,原出处: http://www.gbin1.com/technology/jquery/learningjquerywebsites/ jQuery是目前最流行的 JavaScript    ...

  10. JavaScript一道面试题求y的值是? z 的值是? s的值是?

    原文:http://www.zhufengpeixun.cn/JavaScriptmianshiti/2014-04-01/287.html < script type = "text ...