《第一行代码》第六章 记住密码功能
来源:互联网 发布:吴承恩与西游记 知乎 编辑:程序博客网 时间:2024/05/21 16:21
参考:《第一行代码》
代码搬运工。
前面学了两种数据存储方式,一种是文件一种是SharedPreferences,账户和密码用键值对的方式进行存储和读出比较方便,因此使用了SharedPreferences存储方式进行存储。
编写登陆界面,CheckBox复选框控件,,我改了一下没有用TableLayout布局,而是使用的LinearLayout布局,却依然是只给出height,没想到这里就出错了,在布局预览中就没有出现一个CheckBox控件,我给了width后,才运行正常
这里我们要进行存储的数据简单有三个(不要忘了勾选复选框的数据,要是用户每次都得勾选,那用户体验得有多差)。首先在onCreate()方法中拿到一个SharedPreferences对象,上回说拿对象的方法不要交叉使用,实际上是可以只拿一次 (取出的时候也可以使用这个对象)。在此进行判断,判断复选框是不是曾经被选中,若曾经被选中则返回true,说明之前肯定登陆过且账户和密码已经被存储了,我们只需要拿出来进行显示就OK,同时不要忘了继续把checkbox置为true,(什么时候变成false尼,当然是用户手动取消勾选)。什么时机进行存储才是最佳时机,肯定是在用户执行下一步操作之前的前一个时间(这是XX原则),因此在用户点击登陆按钮的时候就进行存储。
java:
package com.example.databasetest;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.preference.PreferenceManager;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { private EditText edt1, edt2; private CheckBox checkBox; private Button btnloginButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final SharedPreferences preferences = PreferenceManager .getDefaultSharedPreferences(this); //不管三七二十一,我们先拿到SharedPreferences对象再说 edt1 = (EditText) findViewById(R.id.edt_1); edt2 = (EditText) findViewById(R.id.edt_2); checkBox = (CheckBox) findViewById(R.id.checkbox); btnloginButton = (Button) findViewById(R.id.btnlogin); Boolean remember = preferences.getBoolean("Remember", false); //取一下对象,找找checkbox是否(之前)被选中 if (remember) {//曾经被选中(目前也是选中状态),取出数据显示 String accountString = preferences.getString("Account", ""); String passwordString = preferences.getString("Password", ""); edt1.setText(accountString); edt2.setText(passwordString); checkBox.setChecked(true); } /** * 这里是不是可以进行优化,每次点击之后都会进行存储,重复 */ btnloginButton.setOnClickListener(new OnClickListener() { //点击登陆按钮进行存储 @Override public void onClick(View v) { String account = edt1.getText().toString(); String password = edt2.getText().toString(); if (checkBox.isChecked()) { Editor preEditor = preferences.edit(); preEditor.putString("Account", account); preEditor.putString("Password", password); preEditor.putBoolean("Remember", true); preEditor.commit(); } Toast.makeText(MainActivity.this, "You win", Toast.LENGTH_LONG) .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" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="20dp" android:orientation="horizontal" > <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:gravity="right" android:text="@string/account" /> <EditText android:id="@+id/edt_1" android:layout_width="150dp" android:layout_height="wrap_content" android:inputType="none" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="20dp" android:orientation="horizontal" > <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:gravity="right" android:text="@string/passwords" /> <EditText android:id="@+id/edt_2" android:layout_width="150dp" android:layout_height="wrap_content" android:inputType="textPassword" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="20dp" android:orientation="horizontal" > <CheckBox android:id="@+id/checkbox" android:layout_width="30dp" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/remember_passwords" /> </LinearLayout> <Button android:id="@+id/btnlogin" android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/login" /></LinearLayout>
0 0
- 《第一行代码》第六章 记住密码功能
- 第一行代码Andorid——登录记住密码
- 第一行代码第六日
- 《第一行代码》读书笔记(第五章,第六章)
- 【Android】《第一行代码—Android》第六章总结
- 《第一行代码》第六章 文件存储项目Test
- 《第一行代码》第六章 SharedPreferences存储Test
- 《第一行代码》第六章 SQLite存储Test
- 第一行代码笔记,第六章-----详解数据存储
- 第一行代码 第六章 数据储存方案
- 第一行代码 第六章 数据储存方案
- 第一行代码 第六章 数据储存方案
- 第一行代码 第六章 数据储存方案
- 记住密码功能的实现代码
- Android登陆页面记住密码功能代码
- js记住密码功能
- Android 记住密码功能
- 记住密码功能
- 创建OData Service(基于ASP.NET 4.6.1, EF 6),Part I:Project initialize
- Problem D: 栈的基本运算(栈和队列)
- poj 1065 Wooden Sticks
- 数据结构作业-如何求时间复杂度
- 可变参数列表:一个练习题
- 《第一行代码》第六章 记住密码功能
- 浪潮之巅
- 51 nod 一级算法题 1344 走格子
- Hadoop Mapreduce分区、分组、二次排序过程详解
- 关于c++的运算符重载那些事
- C++走向远洋——33(静态成员的应用)
- 华为CodeCraft2016比赛的个人的算法优化
- 数据结构之数组结构(二)
- Problem E: 皇后问题(栈和队列)