android 39 共享首选项
2015-10-18 16:29
363 查看
共享首选项SharedPreferences:用于存储少量数据,大量数据则存入文件或者sd卡。以键值对保存数据。
activity.java
package com.sxt.day06_05; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { EditText metId,metPwd; SharedPreferences mSp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListener(); readData();//自动输入密码 } private void readData() { mSp=getSharedPreferences("login", MODE_PRIVATE); String id=mSp.getString("id", ""); String pwd=mSp.getString("pwd", ""); metId.setText(id); metPwd.setText(pwd); } private void setListener() { findViewById(R.id.btnLogin).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String id=metId.getText().toString(); if(TextUtils.isEmpty(id)){ metId.setError("编号不能为空"); return ; } String pwd=metPwd.getText().toString(); if(TextUtils.isEmpty(pwd)){ metPwd.setError("密码不能为空"); return ; } mSp=getSharedPreferences("login", MODE_PRIVATE);//文件名为login(data/data/com.sxt.day06_05/shared_prefs/login.xml),MODE_PRIVATE表示私有的 Editor editor = mSp.edit();//Editor是内部接口 editor.putString("id", id); editor.putString("pwd", pwd); editor.commit();//存入首选项 Toast.makeText(MainActivity.this, "登陆完成", 2000).show(); } }); } private void initView() { metId=(EditText) findViewById(R.id.etId); metPwd=(EditText) findViewById(R.id.etPwd); } }
页面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <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:text="登陆编号" /> <EditText android:id="@+id/etId" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="2-10个字符" /> </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:text="登陆密码" /> <EditText android:id="@+id/etPwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:password="true" android:hint="2-10个字符" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal"> <Button android:id="@+id/btnLogin" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="20dp" android:text="登陆"/> <Button android:id="@+id/btnExit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="退出" android:layout_marginLeft="20dp" android:layout_marginRight="20dp"/> </LinearLayout> </LinearLayout>
login.xml
相关文章推荐
- Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系
- ANDROID L——RecyclerView,CardView进口和使用(Demo)
- Android基础入门教程——10.2 SmsManager(短信管理器)
- Android Support v4、v7、v13 介绍
- Android--PullToRefreshListView 的简单使用
- Android系统性能优化总结
- android 38 Abdroid客户端和服务端交互
- 关于Android的asynctask-threads-limits问题:asynctask开启的线程是否有极限
- Android通知之自定义通知、响应通知上自定义按钮和更新通知的实现
- Android下拉刷新上拉加载控件,对所有View通用!
- Android-----第三方 ImageLoader 的简单配置和使用
- 使用git查阅Android M中Doze相关代码
- Android应用程序资源文件的编译和打包原理
- Android Studio使用疑难问题汇总
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
- Android 弹出软键盘android:windowSoftInputMode设置总结
- Android 学习笔记之Volley开源框架解析(二)
- Android greenDao的简单配置和使用
- Android通知之通知栏点击通知后返回正在运行的程序,而不是一个新Activity
- android stidio so动态库找不到问题