IOTest把登录信息保存,下次打开自动读取保存的登录信息,无需再次输入
来源:互联网 发布:少儿机器人编程培训 编辑:程序博客网 时间:2024/05/15 15:14
java代码
package com.example.zhang.sharedpreferencestest2;import android.content.SharedPreferences;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends AppCompatActivity { Button mbtnLogin, mbtnCancel; EditText metUserName, metPassWord; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mbtnLogin = (Button) findViewById(R.id.btnLogin); mbtnCancel = (Button) findViewById(R.id.btnCancel); metUserName = (EditText) findViewById(R.id.etUserName); metPassWord = (EditText) findViewById(R.id.etPassWord); MRead(); mbtnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MLogin(); } }); mbtnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } private void MLogin (){ //SharePreference是一个轻量级的存储机制。只能存储一些基本类型,boolean,int,float,long,String //SharePreference本身是一个接口,程序无法直接创建对象 //保存数据的步骤: //1、实例化共享首选项对象(调用接口生成对象,) SharedPreferences spSave = getSharedPreferences("login", MODE_PRIVATE); String UserName = metUserName.getText().toString(); String PassWord = metPassWord.getText().toString(); //SharePreference接口本身没有提供写入数据的能力,而是通过SharePreference的内部接口, //SharePreference调用edit的方法即可获取它所对应的Editor对象 //2、创建编辑对象 SharedPreferences.Editor editor = spSave.edit(); //添加数据 editor.putString("username", UserName); editor.putString("password", PassWord); //保存数据 editor.commit(); } private void MRead(){ //读取数据的步骤 //1、实例化共享首选项对象 SharedPreferences spRead = getSharedPreferences("login", MODE_PRIVATE); //都取数据 //格式:Object value = sp.getXXX(); String UserName_ = spRead.getString("username", "").toString(); String PassWord_ = spRead.getString("password", "").toString(); metUserName.setText(UserName_); metPassWord.setText(PassWord_); }}
xml代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.zhang.sharedpreferencestest2.MainActivity" tools:showIn="@layout/activity_main"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="用户名"/> <EditText android:id="@+id/etUserName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="5" android:hint="请输入用户名"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="密 码"/> <EditText android:id="@+id/etPassWord" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="5" android:hint="请输入密码"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="登陆"/> <Button android:id="@+id/btnCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消"/> </LinearLayout></LinearLayout>
图片
0 0
- IOTest把登录信息保存,下次打开自动读取保存的登录信息,无需再次输入
- IOTest把输入的信息,保存到本地,按读取按钮将其读取出来
- java之cookie保存登录信息下次登录时自动填充表单
- SharedPreference自动保存登录信息
- silverlight保存登录信息
- silverlight保存登录信息
- 登录界面保存信息
- SQLite保存登录信息
- [Android实例] 关于webview如何自动登录保存登录信息
- [Android实例] 关于webview如何自动登录保存登录信息
- webview如何自动登录保存登录信息详情
- 关于webview如何自动登录保存登录信息
- 关于webview如何自动登录保存登录信息
- [Android实例] 关于webview如何自动登录保存登录信息
- 【分享】webview如何自动登录保存登录信息
- android_实现用户登录信息的保存
- 工具类保存登录信息
- 用Cookie 保存登录信息
- 将别处的web project 导入 eclipse 以后,为什么run as里 没有 run on server 选项
- Android Canvas drawArc方法介绍
- 黑马程序员_IO流字节流
- C# 作业,简单的学生管理系统(控制台)
- Nginx负载均衡的配置
- IOTest把登录信息保存,下次打开自动读取保存的登录信息,无需再次输入
- IOTest把输入的信息,保存到本地,按读取按钮将其读取出来
- 在移动跨平台框架PhoneGap中的异步体验
- 从指定文件夹读取文件并把读取到的文件写入到指定文件夹
- HLJU——15级寒假培训3 题解
- SD卡浏览器
- Ubuntu 14.04 没有system settings的解决办法
- 坐标离散化
- AOJ-0531 Paint Color