Android:登录实现记住密码功能
2017-04-12 19:29
796 查看
实现登录界面中记住密码的选项,运用SharedPreferences实现简单的数据存储。
1、登录界面的布局文件:
login_top.xml
界面如下:
2、在LoginActivity中编写代码实现一下 用户存储数据到文件,以及从文件中读取数据的功能:
1、登录界面的布局文件:
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="请输入账号:"> <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="请输入密码:" android:inputType="text"> <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_weight="1" android:layout_height="wrap_content" android:id="@+id/etCheckbook" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:onClick="login" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/btn_select" 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="10dp" android:id="@+id/imageView" /> </RelativeLayout>
界面如下:
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 etCheckbook; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initViews(); sharedPreferences=getSharedPreferences("rememberpassword", Context.MODE_APPEND); boolean isRember=sharedPreferences.getBoolean("rememberpassword",false); if(isRember){ String name=sharedPreferences.getString("name",""); String password=sharedPreferences.getString("password",""); etName.setText(name); etPassword.setText(password); etCheckbook.setChecked(true); } } //获取布局文件中的控件 private void initViews() { etName=(EditText) findViewById(R.id.etName); etPassword=(EditText)findViewById(R.id .etPassword); etCheckbook=(CheckBox)findViewById(R.id.etCheckbook); } //点击登录按钮时获得号码,密码。 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(etCheckbook.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(); } } }
3、点击登陆后跳转到一个欢迎页面,添加一个新的Activity。当第一次输入密码账号登录时选中记住密码,成时功则跳转到欢迎界面,返回手机主界面
时,密码和账号会在上面显示不用再次输入。用SharedPreferences实现了简单的数据存储。
登录成功后,跳转到欢迎界面
记住密码后,再次打开登录界面,账号密码自动显示在上面
相关文章推荐
- Android探索之路:实现登录界面的记住密码功能
- android用SharedPreferences实现登录时记住密码功能
- Android——SharedPreferences实现登录界面的记住密码和自动登录功能
- Android用SharedPreferences保存数据实现注册和登录功能(首次安装默认打开注册,记住密码)
- Android通过SharedPreferences实现自动登录记住用户名和密码功能
- Android实现用户登录记住密码功能
- Android——SharedPreferences实现登录界面的记住密码和自动登录功能
- Android登录界面用SharedPreferences实现记住密码功能
- Android登录界面用SharedPreferences实现记住密码功能
- Android登录界面用SharedPreferences实现记住密码功能
- Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)
- Android实现登录界面记住用户名与密码
- 引用借以记录借鉴 实现记住密码和自动登录功能
- Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)
- 所有系统已经实现单点登录及记住密码功能
- android实现记住用户名和密码以及实现自动登录
- android实现登陆注册记住密码功能
- flex 登录时记住用户名和密码功能实现
- Android App“记住密码”功能的实现逻辑
- 登录时简单的记住密码和自动登录功能实现