用户登录记住密码
来源:互联网 发布:python 不支持中文 编辑:程序博客网 时间:2024/05/01 09:57
要求:
通过代码实现,得到以下效果图
当用户选中了记住密码复选框,并成功登陆一次之后,这个时候如果再重新启动登录界面,之前输入的用户名和密码就会显示在文本框中。
实验步骤:
第一步:实验前准备,相关配置文件以及Activity的建立
actvity_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:background="@drawable/loginbg" 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" 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:id="@+id/imageView" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /></RelativeLayout>
actvity_wel.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="match_parent"> <TextView android:text="Welcome You" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="208dp" android:textSize="25sp" android:id="@+id/welcome" /></RelativeLayout>
代码实现最基本的配置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:background="@drawable/logintop_roungbg" 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> <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:id="@+id/cbPass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="0.26" android:text="@string/btnSave" android:textSize="20sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="1" android:background="@drawable/btn_select" android:onClick="save" android:text="@string/btnLogin" /> </LinearLayout></RelativeLayout>
AndroidMainfest.xml配置文件
代码如下:
<?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=".LoginActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".WelActivity"></activity> </application></manifest>
java下代码:
LoginActivity下代码如下:
package cn.edu.bzu.case_login;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 CheckBox CBPass; private EditText Name; private EditText Password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); this.read(); } public void save(View view) { Name = (EditText) findViewById(R.id.etName); Password = (EditText) findViewById(R.id.etPassword); CBPass = (CheckBox) findViewById(R.id.cbPass); String name = Name.getText().toString(); String password = Password.getText().toString(); Boolean cbpass = CBPass.isChecked(); if (name.equals("admin") && password.equals("123456")) { SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("etName", name); editor.putString("etPassword", password); editor.putBoolean("Click", cbpass); editor.commit(); Intent intent = new Intent(LoginActivity.this, WelActivity.class); startActivity(intent); } else { Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show(); } } public void read() { Name = (EditText) findViewById(R.id.etName); Password = (EditText) findViewById(R.id.etPassword); CBPass = (CheckBox) findViewById(R.id.cbPass); SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE); String name = sp.getString("etName", ""); String password = sp.getString("etPassword", ""); boolean cbpass = sp.getBoolean("Click", false); if (cbpass == true) { Password.setText(password); Name.setText(name); CBPass.setChecked(cbpass); } else { Password.setText(""); Name.setText(name); CBPass.setChecked(cbpass); } }}
WelActivity下代码如下:
package cn.edu.bzu.case_login;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class WelActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wel); }}
实现如图所示代码,然后自己稍加调试,就能得出运行效果图了
0 0
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录(记住密码)
- 用户登录记住密码
- 《大道至简-软件工程实践者的思想》之笔记
- Oracle(26)pl/sql编程 例外处理
- 使用SubVersion+TortoiseSVN多仓库方式进行版本控制
- Java中的注解
- UML学习入门就这一篇文章
- 用户登录记住密码
- mysql学习笔记
- ArchLinux的安装
- Loadrunner小知识积累
- 自定义控件—入门随机数
- thinking in java——0409学习笔记
- OPENCV环境搭建及其原理
- leetcodeOJ 119. Pascal's Triangle II
- java设计模式六大原则(2):里氏替换原则