您的位置:首页 > 移动开发 > Android开发

初学Android——数据储存与访问

2017-04-12 21:02 393 查看
通过实现用户登录账号记住密码,来体验数据的储存与访问实验效果图如下:                                                  当用户勾选记住密码复选框,并成功登陆一次后,这个时候如果再次登陆,之前输入的用户名和密码将出现在文本框中,用户无需再次输入账号密码首先先将实验的样式设计完成,代码如下:
<?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:drawablePadding="10dp"
android:background="@android:drawable/edit_text"
android:ems="10"
android:drawableLeft="@drawable/icon_user"
android:hint="@string/etName">
<requestFocus/>
</EditText>
<EditText
android:id="@+id/etPassword"
android:inputType="textPassword"
android:layout_below="@id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="10dp"
android:background="@android:drawable/edit_text"
android:ems="10"
android:drawableLeft="@drawable/icon_pass"
android:hint="@string/etPass">
<requestFocus/>
</EditText>
<LinearLayout
android:layout_below="@id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="0dp"
android:layout_he
d9d4
ight="wrap_content"
android:layout_weight="1"
android:text="记住密码" />

<Button
android:layout_width="0dp"
android:onClick="login"
android:layout_height="wrap_content"
android:background="@drawable/btn_select"
android:layout_weight="1"
android:text="@string/btn1"/>
</LinearLayout>
</RelativeLayout>
编辑Java代码实现数据存储与访问,代码如下:
package com.example.renxiaohen.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 checkBox;private SharedPreferences sharedPreferences;@Overrideprotected 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);checkBox.setChecked(true);}}private  void initViews(){etName =(EditText)findViewById(R.id.etName);etPassword =(EditText)findViewById(R.id.etPassword);checkBox=(CheckBox)findViewById(R.id.checkBox);}public void  login(View view){String name=etName.getText().toString();String password=etPassword.getText().toString();if("admin".equals(name)&&"123".equals(password)){SharedPreferences.Editor editor =sharedPreferences.edit();if (checkBox.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();}}}
package com.example.renxiaohen.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 checkBox;private SharedPreferences sharedPreferences;@Overrideprotected 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);checkBox.setChecked(true);}}private  void initViews(){etName =(EditText)findViewById(R.id.etName);etPassword =(EditText)findViewById(R.id.etPassword);checkBox=(CheckBox)findViewById(R.id.checkBox);}public void  login(View view){String name=etName.getText().toString();String password=etPassword.getText().toString();if("admin".equals(name)&&"123".equals(password)){SharedPreferences.Editor editor =sharedPreferences.edit();if (checkBox.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();}}}
实现记住密码实验,深刻理解数据存储与访问。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: