用户登录记住密码案例
2017-04-08 14:07
399 查看
用户登录记住密码
1.创建一个 名称为case_login的项目。
2.编辑如图所示的布局界面。
3.创建布局文件login_top.xml
4.编辑LoginActivity.java代码:
1.创建一个 名称为case_login的项目。
2.编辑如图所示的布局界面。
3.创建布局文件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_user" 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/checkbox01" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="记住密码" android:textSize="13sp" /> <Button android:onClick="login" android:id="@+id/denglu01" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:background="@drawable/btn_select" android:text="@string/btnLogin" /> </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: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" android:background="@drawable/login_bg"> <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_marginRight="27dp" android:layout_marginEnd="27dp" android:layout_marginBottom="38dp" android:id="@+id/imageView" /> </RelativeLayout>
4.编辑LoginActivity.java代码:
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; SharedPreferences sharedPreferences; private CheckBox checkbox01; @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); checkbox01.setChecked(true); } } private void initViews() { etName= (EditText) findViewById(R.id.etName); etPassword= (EditText) findViewById(R.id.etPassword); checkbox01=(CheckBox) findViewById(R.id.checkbox01); } public void login(View view){ String name=etName.getText().toString(); String password=etPassword.getText().toString( aec6 ); if("admin".equals(name)&&"123456".equals(password)){ SharedPreferences.Editor editor=sharedPreferences.edit(); if(checkbox01.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.编辑登陆成功界面:
?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="welcome to web!" android:textSize="40sp" android:layout_marginLeft="20dp" android:layout_marginTop="100dp"/> </LinearLayout>5.运行界面如图所示:
相关文章推荐
- Android案例-用户登录记住密码
- 用户登录记住密码案例
- 用户登录记住密码案例
- 用户登录记住密码案例
- 案例------用户登录记住密码
- Android数据存储和访问之SharedPreferences存储方式_用户登录记住密码案例
- 案例------用户登录记住密码
- 公司局域网内访问其他电脑,为了方便,第一次登录时用户名和密码选择了记住,现在想更换用户登陆
- 登录时记住用户名和密码及cookie案例应用
- android开发,以记住用户登录密码为例说明SharedPreferences的简单应用
- 用户登录时的用户名密码加密解密问题和“记住密码”功能
- ios用户登录记住密码
- 这才是一个完整的用户登录(Forms身份认证+记住密码+加密存储+LoginedPage/BasePage)
- android基础-利用SharedPreferences实现用户记住密码和自动登录
- 记一次登录程序的改进过程(改进记住密码功能、提高用户交互)
- 使用jquery.cookie.js实现记住用户信息(下面以web的记住登录名和密码为例进行说明)
- 使用Jquery搭建最佳用户体验的登录页面 之 记住密码自动登录功能
- 记一次登录程序的改进过程(改进记住密码功能、提高用户交互)
- 登录时记住用户名和密码及cookie案例应用
- 记一次登录程序的改进过程(改进记住密码功能、提高用户交互)