初学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();}}}
实现记住密码实验,深刻理解数据存储与访问。
相关文章推荐
- 实验3:android数据储存和访问----登录记住密码功能
- android数据储存和访问-书记的增删改查
- Android(二)数据存储和访问 之文件 1
- Android 学习笔记7---数据存储与访问
- android 数据存储和访问方式五:网络详解
- android通过 httpClient 的post访问 网络数据
- Android 内容提供器---创建内容提供器(Intent对象和数据访问)
- 本人初学设计模式时写的一个支持多数据库的数据访问接口
- android 数据存储和访问方式二:SharedPreferences(共享参数)详解
- android 数据存储和访问方式三:SQLite数据库详解
- 怎么通过web服务器访问MYSQL数据库,使其数据同步到android SQLite数据库?
- Android 应用程序之间数据共享—ContentProvider 保时被访问
- Android 软件开发之数据的 新建 储存 读取 删除 详解(十四)
- Android 软件开发之数据的 新建 储存 读取 删除 详解(十四)
- android 数据存储和访问方式四:ContentProvider(内容提供者、数据共享)详解
- android数据存储与访问
- android 2.3 数据流量开关 移动网络的数据访问功能,非APN开关
- Android深入探究笔记之十 -- 使用 ContentProvider 共享数据(二),访问与添加通讯录
- 怎么通过web服务器访问MYSQL数据库,使其数据同步到android SQLite数据库?
- 5.4、Android中数据存储和访问-使用ContentProvider共享数据