Android数据存储(2)——SharedPreferences
来源:互联网 发布:外媒特朗普访华 知乎 编辑:程序博客网 时间:2024/06/09 21:48
布局
<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/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="UserName" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" /> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="login" /> <Button android:id="@+id/btn_exit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="exit" /></LinearLayout>
MainActivity.java
public class MainActivity extends Activity implements OnClickListener { Button btn_login, btn_exit; EditText et_username, et_password; private String username = "zhangsan"; private String password = "123"; private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } /* * 初始化 */ private void init() { btn_login = (Button) findViewById(R.id.btn_login); btn_exit = (Button) findViewById(R.id.btn_exit); et_username = (EditText) findViewById(R.id.et_username); et_password = (EditText) findViewById(R.id.et_password); btn_login.setOnClickListener(this); btn_exit.setOnClickListener(this); sp = getSharedPreferences("UserInfo", MODE_PRIVATE); // 填充以保存的账号密码 et_username.setText(sp.getString("username", "")); et_password.setText(sp.getString("password", "")); } /* * 监听器 */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_login: String tUserName = et_username.getText() + ""; String tPassword = et_password.getText() + ""; Editor editor = sp.edit(); // String tPassword = et_password.getText().toString(); if (tUserName.equals(username) && tPassword.equals(password)) { // 保存账号密码 editor.putString("username", tUserName); editor.putString("password", tPassword); editor.commit(); Toast.makeText(this, "已登录", Toast.LENGTH_SHORT).show(); } else { et_username.setText(""); et_password.setText(""); // 清空密码 editor.putString("username", ""); editor.putString("password", ""); editor.commit(); Toast.makeText(this, "未登录,账号或密码错误!", Toast.LENGTH_SHORT).show(); } break; case R.id.btn_exit: finish(); break; } }}
0 0
- Android数据存储(2)——SharedPreferences
- Android数据存储——SharedPreferences存储
- Android——SharedPreferences数据存储
- Android——数据存储之SharedPreferences
- Android数据存储——SharedPreferences
- Android存储数据方法——SharedPreferences
- Android数据存储 —— SharedPreferences
- Android——SharedPreferences存储数据方式
- Android数据存储——SharedPreferences
- Android数据存储之——SharedPreferences
- 【笔记】Android数据存储——SharedPreferences
- Android入门(16)——数据存储之SharedPreferences
- 关于Android数据存储方式(一)——SharedPreferences
- 传智播客—Android(三) 数据存储之二 SharedPreferences
- Android数据存储——1.使用SharedPreferences存储数据
- Android中数据存储——SharedPreferences存储数据
- Android 数据存储操作(1)——SharedPreferences存储
- Android的数据存储——存储键值对,SharedPreferences
- 待解决问题集锦
- 【contentprovider系列5】修改联系人信息
- LeetCode143之ReorderList的Java题解
- 第8周项目1(2)实现复数类中的运算符重载(友元函数)
- WebRTC代码走读(六):视频发送流程
- Android数据存储(2)——SharedPreferences
- 关于matlab中的手写字体识别 的mat文件
- google-hack相关的python脚本
- Linux应用程序之信号量
- linux命令学习笔记
- 【VR】Leap Motion 官方脚本手册翻译之---HandController(手型控制器)
- 二叉树的建立和递归遍历、非递归遍历操作
- struts2学习笔记之十(用户选择语言环境小程序)
- P123 29