您的位置:首页 > 其它

用户登录记住密码案例

2017-04-08 14:07 399 查看
用户登录记住密码
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.运行界面如图所示:

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: