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的更多相关文章
随机推荐
- Parallel for loops in .NET C# z
The start index: this is inclusive, i.e. this will be the first index value in the loop The end inde ...
- Ofbiz 10.04 + eclipse 安装与配置
1.下载 ofbiz 10.04:http://ofbiz.apache.org/download.html: 2.下载 freemarker-2.3.15 eclipse 插件(FreeMarker ...
- HW6.28
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- how to learn device driver
making a linux usb driver http://www.kroah.com/linux/ http://matthias.vallentin.net/blog/2007/04/wri ...
- A Tour of Go Map literals
Map literals are like struct literals, but the keys are required. package main import "fmt" ...
- Python解释器
当我们编写Python代码时,我们得到的是一个包含Python代码的以.py为扩展名的文本文件.要运行代码,就需要Python解释器去执行.py文件. 由于整个Python语言从规范到解释器都是开源的 ...
- nyoj 845 无主之地1
无主之地1 时间限制:1000 ms | 内存限制:65535 KB 难度:0 描述 子晓最近在玩无主之地1,他对这个游戏的评价不错,结合了FPS与RPG元素,可玩度很高.不过,他发现了一代的 ...
- 转载SSIS中的容器和数据流—举例说明数据转换任务
在上一个随笔中我们熟悉了数据流任务,现在来做一个例子,通过实践学习这些介绍的内容.这个例子从AdventureWorks数据库中取得数据,然后对数据进行聚合,排序,计算产生新列操作并输入到一个.csv ...
- 7个改变世界的Java项目
Java的开源生态系统是强大而健康的,这是我们(Oreilly)创建OSCON Java(Open Source Convention Java)的主要原因之一.在过去10年中,一些项目已经被广泛接受 ...
- Spring Data JAP 多个不是必填的查询条件处理
简单的介绍一下使用场景,DAO层用Spring Data实现,dao 只有接口,实现类是容器启动时动态字节码生成,接口里定义方法,方法上@Query 里写JPQL查询语句. 基于以上的限制,如果对一个 ...