android登录密码自动保存,Android实现登录界面记住密码的存储

Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences, 其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入。所以比较适合我们今天做的这个项目。我们来看一下运行图:

e77e650dd5751cfefd644a5d18b6d021.png

一.布局界面

1.login_top.xml

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="@dimen/activity_horizontal_margin"

android:background="@drawable/logintop_roundbg">

android:id="@+id/etName"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10"

android:drawablePadding="10dp"

android:background="@android:drawable/edit_text"

android:drawableLeft="@drawable/icon_user"

android:hint="@string/etName">

android:id="@+id/etPassword"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/etName"

android:inputType="textPassword"

android:ems="10"

android:drawablePadding="10dp"

android:background="@android:drawable/edit_text"

android:drawableLeft="@drawable/icon_pass"

android:hint="@string/etpassword">

android:id="@+id/cbremenber"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/etPassword"

android:text="@string/cbpass"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/cbremenber">

android:id="@+id/btnlogin"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="@drawable/btnselect"

android:text="@string/btnlogin"

android:onClick="login"/>

android:id="@+id/btnRegister"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="@drawable/btnselect"

android:text="@string/btnRegister"

android:layout_marginLeft="10dp"/>

2.activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/loginbg"

tools:context="cn.edu.bzu.logindemo.MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/deer"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true"

android:layout_alignParentEnd="true" />

3.activity_welcome.xml

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_welcome"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="cn.edu.bzu.logindemo.WelcomeActivity">

android:id="@+id/tvwelcome"

android:text="Welcome you"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_marginTop="200dp"

android:textSize="40sp"

/>

二.MainActivity

public class MainActivity extends AppCompatActivity {

private EditText etName;

private EditText etPassword;

private SharedPreferences sharedPreferences;

private CheckBox cbremenber;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initViews();

sharedPreferences=getSharedPreferences("remenberpassword", Context.MODE_PRIVATE);

boolean isRemember=sharedPreferences.getBoolean("remenberpassword",false);

if(isRemember) {

String name = sharedPreferences.getString("name", "");

String password = sharedPreferences.getString("password", "");

etName.setText(name);

etPassword.setText(password);

cbremenber.setChecked(true);

}

}

private void initViews() {

etName=(EditText) findViewById(R.id.etName);

etPassword=(EditText) findViewById(R.id.etPassword);

cbremenber=(CheckBox)findViewById(R.id.cbremenber);

}

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(cbremenber.isChecked()){

editor.putBoolean("remenberpassword",true);

editor.putString("name",name);

editor.putString("password",password);

}else {

editor.clear();

}

editor.commit();

Intent intent=new Intent(this,WelcomeActivity.class);

startActivity(intent);

finish();

}else {

Toast.makeText(this,"账号或密码有误",Toast.LENGTH_LONG).show();

}

}

}

三.WelcomeActivity

public class WelcomeActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_welcome);

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值