sharedpreference入门
来源:互联网 发布:淘宝商品已过期 编辑:程序博客网 时间:2024/06/10 18:22
MainActivity.java
package com.example;import java.util.Map;import com.example.service.loginService;import android.support.v7.app.ActionBarActivity;import android.text.TextUtils;import android.util.Log;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends ActionBarActivity { private static final String TAG = "MainActivity"; //日志 一般是类名"MainActivity" private EditText et_username; private EditText et_password; private CheckBox cb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_username = (EditText)this.findViewById(R.id.et_username); et_password = (EditText)this.findViewById(R.id.et_password); cb = (CheckBox) findViewById(R.id.cb_remember_pwd); SharedPreferences sp = getSharedPreferences("config",MODE_PRIVATE ); String username = sp.getString("username", ""); et_username.setText(username); String password = sp.getString("password", ""); et_password.setText(password); } public void login(View view){ String username = et_username.getText().toString().trim(); String password = et_password.getText().toString().trim(); if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){ Toast.makeText(this, "用户名或密码不能为空", 1).show(); }else{ //判断是否保存密码 if(cb.isChecked()){ //如果勾选记住密码 Log.i(TAG, "需要保存用户名密码"); //日志 loginService.saveUserInfo(this,username, password); Toast.makeText(this, "保存用户信息成功", 0).show(); } } //登录 发送消息到服务器,服务器验证是否成功 if("1477996221".equals(username)&&"666666".equals(password)){ Toast.makeText(this, "登陆成功", 1).show(); } else{ Toast.makeText(this, "登录失败,账号或密码错误",1).show(); } } }
/res/layout/activity_main.xml
<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="com.example.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号" /><EditText android:id="@+id/et_username" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" /><EditText android:inputType="textPassword" android:id="@+id/et_password" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"><CheckBox android:id="@+id/cb_remember_pwd" android:checked="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" /><Button android:onClick="login" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" /></RelativeLayout></LinearLayout>
loginSrvice.java 右击 原有包 新建一个package(com.example.service)
右击 新建包 新建一个class(loginSrvice)
package com.example.service; //新建包名import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.net.ContentHandler;import java.text.BreakIterator;import java.util.HashMap;import java.util.Map;import org.apache.http.entity.InputStreamEntity;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;public class loginService { /* ***保存账号密码的业务方法*** * content 上下文 * username 账号 * password 密码 */ public static void saveUserInfo(Context context, String username, String password){ SharedPreferences sp = context.getSharedPreferences("config",context.MODE_PRIVATE ); Editor editor = sp.edit();//得到一个sp的编辑器 editor.putString("username", username); editor.putString("password", password); editor.commit(); //类似于数据库的事务 保证数据同时提交成功}}
阅读全文
1 0
- sharedpreference入门
- Sharedpreference
- SharedPreference
- SharedPreference
- SharedPreference
- SharedPreference
- SharedPreference
- SharedPreference
- SharedPreference
- SharedPreference
- SharedPreference
- SharedPreference
- SharedPreference
- SharedPreference
- Android--入门---17---(Android sharedPreference --用户偏好)
- SharedPreference 用法
- android sharedPreference
- SharedPreference介绍
- pxe全自动化安装虚拟机
- CSharp直接连接MySQL
- 为什么C++的SEH不提供finally
- Hexo博客添加SEO-评论系统-阅读统计-站长统计
- openGL ES进阶教程(五)制作一个简单的VR播放器,播放全景视频
- sharedpreference入门
- 推荐给大家一个优惠卷搜索网站
- java分布式服务框架Dubbo的介绍与使用
- amazeui表单提交实例
- hadoop ha配置后一个namenode不能自动failover,相当于没有配置ha
- 设计模式之禅笔记-适配器模式
- HDU6063 [2017多校联合3] RXD and math 打表 快速幂
- Handler机制更新UI线程控件
- Java二维数组