《第一行代码》第六章 记住密码功能
2016-04-11 21:55
211 查看
参考:《第一行代码》
代码搬运工。
前面学了两种数据存储方式,一种是文件一种是SharedPreferences,账户和密码用键值对的方式进行存储和读出比较方便,因此使用了SharedPreferences存储方式进行存储。
编写登陆界面,CheckBox复选框控件,,我改了一下没有用TableLayout布局,而是使用的LinearLayout布局,却依然是只给出height,没想到这里就出错了,在布局预览中就没有出现一个CheckBox控件,我给了width后,才运行正常
这里我们要进行存储的数据简单有三个(不要忘了勾选复选框的数据,要是用户每次都得勾选,那用户体验得有多差)。首先在onCreate()方法中拿到一个SharedPreferences对象,上回说拿对象的方法不要交叉使用,实际上是可以只拿一次 (取出的时候也可以使用这个对象)。在此进行判断,判断复选框是不是曾经被选中,若曾经被选中则返回true,说明之前肯定登陆过且账户和密码已经被存储了,我们只需要拿出来进行显示就OK,同时不要忘了继续把checkbox置为true,(什么时候变成false尼,当然是用户手动取消勾选)。什么时机进行存储才是最佳时机,肯定是在用户执行下一步操作之前的前一个时间(这是XX原则),因此在用户点击登陆按钮的时候就进行存储。
java:
布局文件
代码搬运工。
前面学了两种数据存储方式,一种是文件一种是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>
相关文章推荐
- JNI中javah找不到类文件解决方法
- 【java】匿名内部类实例
- PHP移动文件指针ftell()、fseek()、rewind()函数总结
- C++作业3
- struts2 笔记 1
- 使用python抓取婚恋网用户数据并用决策树生成自己择偶观
- java的左移和右移的区别常见的hascode和equals写法
- java从控制台输入数值之System.in.read()
- 初学JAVA,开发环境的搭建(JDK和Eclipse的安装)
- Autolayout约束的代码实现方式-1
- java提高篇(八)----详解内部类
- javaweb中如果用户没有登录,不能访问别的页面,只能访问登录页面
- 关于Spring常用的注解
- [c++] C++11 Signals and Slots
- Java入门 第一季第四章 流程控制语句
- c++第三次实验
- Matlab Language Index
- C语言基础篇(2):按位比较是否相等、找出不是成对出现的单数、不用中间变量实现两个数的交换
- c++第三次上机实验-个人所得税计算器
- java获取客户端ip 及通过ip获取mac地址后台代码(借鉴别人)