归档—SharedPreferences(轻量级存储)
来源:互联网 发布:好的手机壁纸软件 编辑:程序博客网 时间:2024/06/06 20:20
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">SharedPreferences(轻量级的存储),通常我们可以用它来记录一些小的数据,例如我们可以记录用户的账号及密码,以便用户下次登录不需要重复输入。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">示例代码:</p>
import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener{private EditText pass;private EditText user;private SharedPreferences sp;private String strPass;private String strUser;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button but= (Button) findViewById(R.id.but);but.setOnClickListener(this);pass = (EditText) findViewById(R.id.edi_pass);user = (EditText) findViewById(R.id.edi_user);sp = this.getSharedPreferences("sp", Context.MODE_PRIVATE);String u = sp.getString("user","");String p = sp.getString("pass", null);if("".equals(u) || p != null){user.setText(u);pass.setText(p);}}@Overridepublic void onClick(View v) {strPass = pass.getText().toString().trim();strUser = user.getText().toString().trim();Toast.makeText(this, "账号:"+strUser+" 密码:"+strPass, Toast.LENGTH_SHORT).show();if ("123".equals(strUser) && "12345".equals(strPass)) {Toast.makeText(this, "输入正确", Toast.LENGTH_SHORT).show();Editor edi=sp.edit();boolean is=sp.getBoolean("isf", true);if (is) {edi.putBoolean("isf", false);}else{}edi.putString("pass", strPass);edi.putString("user", strUser);edi.commit();}else{Toast.makeText(this, "输入错误", Toast.LENGTH_SHORT).show();}}}
布局:
<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" > <EditText android:id="@+id/edi_user" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="账号" /> <EditText android:id="@+id/edi_pass" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="密码" /> <Button android:id="@+id/but" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" /></LinearLayout>
0 0
- 归档—SharedPreferences(轻量级存储)
- SharedPreferences轻量级数据存储
- Android sharedpreferences轻量级存储
- SharedPreferences轻量级存储
- 【数据存储】sharedPreferences-轻量级存储
- 轻量级数据存储方式--SharedPreferences
- 轻量级SharedPreferences的存储方式
- Android中一个轻量级的存储方案(SharedPreferences)
- [Android] SharedPreferences(轻量级的存储方式)
- SharedPreferences轻量级存储工具类--方便使用
- 轻量级文件存储SharedPreferences使用管理类
- SharedPreferences(轻量级的存储)-->简单应用
- [Android] SharedPreferences(轻量级的存储方式)
- SharedPreferences(偏好设定;轻量级的存储)
- 数据存储:轻量级存储类SharedPreferences的一个应用实例
- 处女男学Android(十三)---Android 轻量级数据存储之SharedPreferences
- Android轻量级的存储SharedPreferences及getSharedPreferences 、 getPreferences 与getDefaultSharedPreferences
- Android存储(一)——SharedPreferences
- 无限互联 奖学金 连载1北京总部 49期学员肖建祥
- TOJ 3858 Home 搜索
- MFC资源切换(AFX_MANAGE_STATE)简介
- C语言中的数组和字符串
- 循环有序数组的查找
- 归档—SharedPreferences(轻量级存储)
- (面试加分题)OC中block的基本用法与深入了解
- HDU 1003 Max Sum
- 程序设计项目实训——银行储蓄系统
- 数据库事务隔离级别与锁
- [LeetCode][Java] Rotate List
- 13个js图表插件
- gdb基本命令
- poj 2309 BST