Android案例-用户登录记住密码

来源:互联网 发布:后窗知乎 编辑:程序博客网 时间:2024/04/26 03:29

1.创建程序

该程序用于存储信息,记住密码功能,避免再次登录输入密码的麻烦;

(1)创建一个名为“case_login”的应用程序,包名为cn.edu.bzu.case_login,修改界面显示名为“登录界面”;

代码如下:

<string name="app_name">登录界面</string>

2.设计用户交互界面

(1)在drawable文件夹下导入图片和布局文件中相同的代码


(2)“登录界面”程序对应的layout文件夹下的三个布局文件

*布局文件(login_top.xml),相对布局其下的两个文本控件主要用于放置登录所输入的用户名和密码, 线性布局(LinearLayout)下方设置一个按钮(Button)用于单击保存用户名和密码,和一个CheckBox控件用来记住密码;代码如下: 
<?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/checkBox"            android:layout_weight="1" />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:layout_marginLeft="10dp"            android:background="@drawable/btn_select"            android:text="登录" />    </LinearLayout></RelativeLayout>

*布局文件(activity_login.xml),该文件运用include控件包含login_top.xml基础上添加背景和图片控件(ImageView)等;

代码如下:

<?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_centerVertical="true"        android:layout_centerHorizontal="true"        android:textSize="40dp"        android:textColor="#0f0"        android:layout_margin="263dp"        android:id="@+id/textView" /></RelativeLayout>
 3.编写界面交互代码,创建私有工具类
*在initView()方法中获取到控件;
*在login方法中实现了单击“登录”按钮时,使用SharePreferences()成功地将name和pass保存到了data.xml文件中,
运用gettext()获取原登录记住的密码;
*为了程序的健壮性,加入了判断,检查用户名或密码,如果错误或为空,则弹出一个Toast提示,账号或密码有误;
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.Checkable;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends AppCompatActivity {    private EditText etName;    private EditText etPassword;    private SharedPreferences sharedPreferences;    private Checkable ebISRemeberPass;    @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);            ebISRemeberPass.setChecked(true);        }    }    private void initViews(){        etName=(EditText)findViewById(R.id.etName);        etPassword=(EditText)findViewById(R.id.etPassword);    }    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 (ebISRemeberPass.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();        }    }}
4.运行程序界面登录
程序使用说明:
*运行程序,显示界面(1);



*输入正确的用户和密码,然后选择记住密码,单击“ 登录”按钮,界面(2);



*会弹出登录成功,界面(3)




 

0 0
原创粉丝点击