SharedPreferences实现记住密码功能
来源:互联网 发布:宅男看片软件免费 编辑:程序博客网 时间:2024/05/12 15:18
SharedPreferences接口非常适合用来存储零散的数据。这里我们用来实现记录用户名和密码的功能。在前面我用过IO流来实现记住密码的功能。那么用SharedPreferences接口会比用IO流更加方便,代码更加简洁。也更高效。
首先编写顶部布局,代码如下:
<?xml version="1.0" encoding="utf-8"?>
< RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android" //相对布局
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/logintop_roundbg" //背景样式设置
android:padding="@dimen/activity_horizontal_margin">
<EditText //文本框
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/edit_text" //样式设置
android:drawableLeft="@drawable/icon_user" //图片放置
android:drawablePadding="10dp"
android:ems="10"
android:hint="@string/etName" /> //提示字
<requestFocus />
<EditText
android:id="@+id/etPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etName"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/icon_pass"
android:drawablePadding="10dp"
android:ems="10"
android:hint="@string/etPassword"
android:inputType="textPassword" />
<requestFocus />
<LinearLayout //线性布局
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etPass">
<CheckBox //复选框
android:text="记住密码"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/rememberpass"
android:layout_weight="1"
android:textSize="20sp"/>
<Button //按钮
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/btn_select"
android:text="@string/btnLogin"
android:onClick="login"/> //点击事件
</LinearLayout>
< /RelativeLayout>
首先编写顶部布局,实现登录的布局,代码如下:
<?xml version="1.0" encoding="utf-8"?>
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" //相对布局
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/loginbg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.edu.bzu.case_login.Login_Activity">
<include //把顶部布局包含进来
android:id="@+id/include1"
layout="@layout/login_top"></include> //布局名字
<ImageView //view控件
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
app:srcCompat="@drawable/deer" /> //图片引用
< /RelativeLayout>
跳转的成功界面,代码如下:
<?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" //线性布局
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView //textview控件
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="welcome you" //文本
android:textSize="50sp"
android:layout_gravity="center"
android:layout_marginTop="100dp"/>
< /LinearLayout>
package cn.edu.bzu.case_login;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class Login_Activity extends AppCompatActivity {
private EditText etname; //私有变量声明
private EditText etpass;
private SharedPreferences sharedPreferences;
private CheckBox rememberpass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initviews(); //绑定控件的方法
sharedPreferences = getSharedPreferences("remember", Context.MODE_PRIVATE); //调 用 getSharedPreferences实现一个sharedPreferences
boolean remember=sharedPreferences.getBoolean("remember",false); //读取sharedPreferences中的数据看数据上次是否被记住了
if (remember){ //如果记住了
String name=sharedPreferences.getString("name",""); //读取sharedPreferences的数据
String pass=sharedPreferences.getString("pass","");
etname.setText(name); //把数据放入文本框
etpass.setText(pass);
rememberpass.setChecked(true); //复选框被勾选
}
}
private void initviews() { //绑定控件的方法编写
etname = (EditText) findViewById(R.id.etName);
etpass = (EditText) findViewById(R.id.etPass);
rememberpass = (CheckBox) findViewById(R.id.rememberpass);
}
public void login(View view) { //登录事件的编写
String name = etname.getText().toString(); //获取文本框的输入的数据
String pass = etpass.getText().toString();
if ("admin".equals(name) && "123456".equals(pass)) { //用输入的数据与正确数据进行比较,是否可以登录
SharedPreferences.Editor editor = sharedPreferences.edit(); // 声明一个文本编辑器
if (rememberpass.isChecked()) { //判断复选框是否选中
editor.putBoolean("remember",true); //把数据放入editor文本编辑器
editor.putString("name",name);
editor.putString("pass",pass);
}else{
editor.clear(); //没选中清除editor文本编辑器中的数据
}
editor.commit(); //把editor文本编辑器中的数据存储
Intent intent=new Intent(this,MainActivity.class); //跳转到另一个Activity
startActivity(intent); //开始Activity
finish(); //结束
}else {
Toast.makeText(this,"账号或密码错误",Toast.LENGTH_LONG).show(); //未登录成功,用吐司显示文本
}
}
}
编写跳转的Activity,代码:
package cn.edu.bzu.case_login;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by ASUS on 2017/4/8.
*/
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
最后在Android配置文件中添加Activity,代码如下:
<?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.bzu.case_login">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Login_Activity"> //声明Activity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" //声明Activity
android:label="MainActivity">
</activity>
</application>
< /manifest>
- SharedPreferences实现记住密码功能
- 使用SharedPreferences实现记住密码功能
- 利用SharedPreferences实现记住密码功能
- 使用SharedPreferences实现登录记住密码功能实现
- 使用 SharedPreferences 实现记住用户名和密码功能
- 实现用户名及密码记住功能(SharedPreferences的用法)
- Android-利用SharedPreferences实现记住密码和账号功能
- android用SharedPreferences实现登录时记住密码功能
- 实践--使用SharedPreferences实现记住密码的功能
- 使用SharedPreferences实现记住账号密码的功能
- Android登录界面用SharedPreferences实现记住密码功能
- Android登录界面用SharedPreferences实现记住密码功能
- Android登录界面用SharedPreferences实现记住密码功能
- 使用SharedPreferences实现简单的记住用户名和密码功能
- 通过SharedPreferences实现记住密码
- 使用SharedPreferences记住密码的功能
- 实现记住密码功能
- SharedPreferences实现登录界面记住密码
- Android 通过包名打开APP
- (二)数组是一种数据类型
- mysql导入excel数据乱码问题
- HDU
- js无缝滚动
- SharedPreferences实现记住密码功能
- ie8/9收到json数据后提示下载的解决方法
- Ext JS多语言
- 记住密码界面
- Android View框架的measure机制
- 类的初体验
- iOS UIKit框架注解
- 用户登录记住密码
- unity中开启和关闭协同程序