用户登录记住密码
来源:互联网 发布:oracle数据库备注乱码 编辑:程序博客网 时间:2024/05/01 06:13
通过SharedPreferences的学习,实现如下图所示界面:
功能是:当用户选中了记住密码复选框,并成功登录一次之后,这个时候如果再重新启动登陆界面,之前输入的用户名和密码就会显示在文本框中。
实现该案例的具体步骤如下:
1.创建程序
创建一个名为”Case_Login”的应用程序,包名为cn.edu.bzu.case_login。设计用户交互界面,如图所示:
对应布局文件(login_top.xml)如下:
<?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:padding="@dimen/activity_horizontal_margin" android:background="@drawable/logintop_roundbg"> <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> <EditText android:id="@+id/etPassword" 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/etPass" android:inputType="textPassword"> <requestFocus /> </EditText> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/etPassword"> <CheckBox android:text="记住密码" android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/cbIsRememberPass" android:textSize="20sp" android:layout_weight="1" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/btn_select" android:onClick="login" android:text="@string/btnLogin" /> </LinearLayout></RelativeLayout>
对应布局文件(activity_login.xml)如下:
<?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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@drawable/loginbg" tools:context="cn.edu.bzu.case_login.LoginActivity"> <include layout="@layout/login_top"></include> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/deer" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginBottom="20dp" android:id="@+id/imageView" /></RelativeLayout>
对应布局文件(activity_main.xml)如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" 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.MainActivity"> <TextView android:text="Welcome you" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="50sp" android:layout_marginTop="260dp" android:id="@+id/textView" /></RelativeLayout>
上述代码中,第一个布局文件主要用于放置登录所输入的用户名和密码,用户名和密码条目又分别放置了一个TextView和一个EditText。TextView用于提示用户名和密码的文字,EditText用于输入。最后在下方分别设置了一个单选框CheckBox和一个按钮Button,单选框CheckBox用于记住密码,按钮Button用于登录。第二个布局文件主要用来设置包含第一个布局文件的背景。第三个布局文件主要用来设置Toast,使其登录成功后显示Welcome you。
2.编写界面交互代码(LoginActivity)
代码如下:
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 LoginActivity extends AppCompatActivity { private EditText etName; private EditText etPassword; private CheckBox cbIsRememberPass; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initViews(); sharedPreferences=getSharedPreferences("rememberpassword", Context.MODE_PRIVATE); boolean isRemember=sharedPreferences.getBoolean("rememberpassword",false); if(isRemember) { String name = sharedPreferences.getString("name", ""); String password = sharedPreferences.getString("password", ""); etName.setText(name); etPassword.setText(password); cbIsRememberPass.setChecked(true); } } private void initViews(){ etName=(EditText)findViewById(R.id.etName); etPassword=(EditText)findViewById(R.id.etPassword); cbIsRememberPass=(CheckBox)findViewById(R.id.cbIsRememberPass); } public void login(View view){ String name=etName.getText().toString(); String password=etPassword.getText().toString(); if("admin".equals(name)&&"123456".equals(password)){ SharedPreferences.Editor editor=sharedPreferences.edit(); if(cbIsRememberPass.isChecked()){ editor.putBoolean("rememberpassword",true); editor.putString("name",name); editor.putString("password",password); }else{ editor.clear(); } editor.commit(); Intent intent=new Intent(this,MainActivity.class); startActivity(intent); finish(); }else { Toast.makeText(this,"账号或密码有误",Toast.LENGTH_LONG).show(); } }}
上述代码中,首先在initView()方法中获取控件,然后再onClick()方法中单击登录,当登录成功后会弹出一个Toast提示。
3.运行程序登录记住密码
运行效果图如下:
当点击记住密码登录后关闭,再次进入时将不用重新输入用户名和密码。
0 0
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录(记住密码)
- 用户登录记住密码
- MFC does not support WINVER less than 0x0501 解决方案
- 案例------用户登录记住密码
- MacBook OS操作快捷键
- tomcat配置https单向和双向认证
- Java对象及其引用
- 用户登录记住密码
- 教程:盲打入门常识.
- datagridview 自动列宽
- docker安装activemq
- Redis集群
- (python)输入一个三位数以上的整数,输出其百位以上的数字
- 《Effective Java》第8章 通用程序设计
- 524
- poj 1284 求一个数的原根个数