Android笔记 SharedPreferences demo
来源:互联网 发布:单片机433接收程序 编辑:程序博客网 时间:2024/06/05 08:23
1布局
<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=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入用户名" /> <EditText android:id="@+id/etusername" 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:id="@+id/etpass" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textPassword" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <CheckBox android:id="@+id/cb_rem_pass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="记住密码" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:onClick="login" android:text="登陆" /> </RelativeLayout></LinearLayout>
2代码
MainActivity.java
package com.example.a24_logform;import com.example.a24_logform.lognservice.LoginService;import com.example.a28_sharedpreference.R;import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private static final String TAG="MainActivity";EditText editname =null;EditText editpass=null;CheckBox checkBox=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editname=(EditText) this.findViewById(R.id.etusername);editpass=(EditText) this.findViewById(R.id.etpass);checkBox=(CheckBox) this.findViewById(R.id.cb_rem_pass);SharedPreferences sp= getSharedPreferences("config", MODE_PRIVATE);String name=sp.getString("name", "");editname.setText(name);String pass=sp.getString("pass", "");editpass.setText(pass);}public void login(View view){String nameString=editname.getText().toString().trim();String passString=editpass.getText().toString().trim();if(TextUtils.isEmpty(nameString)||TextUtils.isEmpty(passString)){Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();}else{if(checkBox.isChecked()){Log.i(TAG, "要保存密码");Toast.makeText(this, "记住密码", Toast.LENGTH_LONG).show();LoginService.saveUserInfo(this,nameString, passString);Toast.makeText(this, "保存成功", Toast.LENGTH_LONG).show();}if("zhangsan".equals(nameString)&&"123".equals(passString)){Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG).show();}else{Toast.makeText(this, "登陆失败,用户名或密码错误", Toast.LENGTH_LONG).show();}}}}
LoginService.java
package com.example.a24_logform.lognservice;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;public class LoginService {public static void saveUserInfo(Context context, String name,String pass){SharedPreferences sp=context.getSharedPreferences("config", Context.MODE_PRIVATE);Editor editor= sp.edit();editor.putString("name", name);editor.putString("pass", pass);//采用类似事物的回滚 保证数据同时提交editor.putInt("count", 23);editor.putFloat("PI", 3.1415f);editor.commit();}}
0 0
- Android笔记 SharedPreferences demo
- SharedPreferences---Android学习笔记
- Android SharedPreferences笔记
- Android学习笔记-SharedPreferences
- Android学习笔记-SharedPreferences
- Android SharedPreferences 用法笔记简介
- Android学习笔记12:SharedPreferences
- Android笔记--------SharedPreferences的Editor
- Android开发-Sharedpreferences-存储数据使用方法-完整Demo-AndroidStudio
- Android笔记 simpleAdapter demo
- Android(OPhone) 学习笔记 - SharedPreferences
- android学习笔记(八)SharedPreferences、editor
- android学习笔记(八)SharedPreferences、editor
- Android学习笔记<20140112> SharedPreferences的使用
- android开发笔记之SharedPreferences的使用
- Android 学习笔记 第五季 9 SharedPreferences
- Android入门笔记 - 数据存储 - SharedPreferences
- Android笔记使用SharedPreferences进行数据存储
- 多线程之经典生产者消费者问题
- 神翻译
- Java的Thread.setDaemon
- Twitter实时搜索系统EarlyBird的总结
- 只为完成作业
- Android笔记 SharedPreferences demo
- solr 定时创建全量索引和增量索引
- web.py指南性说明
- 朝着目标前进,怎么能在途中放弃
- C++实现队列输入输出
- const 的用法大全
- 正向代理 反向代理
- XML CDATA的作用
- java中system.arraycopy