您的位置:首页 > 编程语言

《第一行代码》第六章 记住密码功能

2016-04-11 21:55 211 查看
参考:《第一行代码》

代码搬运工。

前面学了两种数据存储方式,一种是文件一种是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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: