数据存储:轻量级存储类SharedPreferences的一个应用实例
来源:互联网 发布:公司淘宝培训协议书 编辑:程序博客网 时间:2024/05/23 23:12
效果描述
使用SharedPreferences实现一个简单的数据存储,在该页面中可以输入姓名和邮箱,有三个按钮分别是:提交,查看,清空。
提交按钮用于将数据保存在指定的文件中;
查看按钮用于查看当前文件中保存的数据;
清空按钮用于清楚文件中的内容;
具体效果如下所示:
使用方法
SharedPreferences用于将数据保存到文件中,即使退出程序后,下次再次依旧能查看到之前保存过的数据,这体现了数据的持久化SharedPreferences作为一个轻量级的存储类,可以保存 简单的key-value信息,实现SharedPreferences的存储步骤如下:
- 使用Activity类的个体SharedPreferences()方法获得SharedPreferences对象。其中存储key-value文件的名称由getSharedPreferences()方法中的第一个参数指定。
SharedPreferences shared=getSharedPreferences(PREFERENCE_NAME, MODE);
2.使用 例如:SharedPreferences接口的edit()方法获取Editor对象;
Editor eidtor=shared.edit(); //声明Editor对象
3.通过Editor对象的putXXX()方法存储key-value键值对数据。其中XXX表示value的不同数据类型。通过commit()方法提交数据
eidtor.putString("name", name.getText().toString());eidtor.putString("email", email.getText().toString());eidtor.commit(); //编辑完成,保存
静态页面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" 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" tools:context=".MainActivity" > <TextView android:id="@+id/wname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名" android:textSize="22dp" android:layout_marginTop="5dp" android:textColor="#000000" /> <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:layout_marginTop="5dp" > <requestFocus /> </EditText> <TextView android:id="@+id/wemail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="邮箱" android:textSize="22dp" android:layout_marginTop="5dp" android:textColor="#000000"/> <EditText android:id="@+id/email" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textEmailAddress" android:layout_marginTop="5dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="5dp"> <Button android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交"/> <Button android:id="@+id/look" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查看" /> <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除" /> </LinearLayout> <View android:layout_marginTop="5dp" android:layout_width="220dp" android:layout_height="3dp" android:background="#666666"/> <TextView android:layout_marginTop="5dp" android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="在此查看您的内容" android:textAppearance="?android:attr/textAppearanceLarge" /></LinearLayout>
事件响应
package com.example.sharedpreferencedemo;/* * 使用SharedPreferences存储简单的数据并读取 */import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.view.Gravity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity { public static int MODE=MODE_PRIVATE; //访问模式 public static final String PREFERENCE_NAME="test1"; //保存文件的名称 private EditText name; private EditText email; private Button submit; private Button look; private Button clear; private TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name=(EditText) findViewById(R.id.name); email=(EditText) findViewById(R.id.email); submit=(Button) findViewById(R.id.submit); look=(Button) findViewById(R.id.look); clear=(Button) findViewById(R.id.clear); show=(TextView) findViewById(R.id.show); //获取对象 final SharedPreferences shared=getSharedPreferences(PREFERENCE_NAME, MODE); //为查看按钮添加事件监听,查看文件中的内容 look.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String nameLook=shared.getString("name", "暂无"); //key-value String emailLook=shared.getString("email","暂无"); show.setText("姓名:"+nameLook+"\n"+"邮箱:"+emailLook+"\n"); clearEdit(); // 清空编辑框的内容 } }); //为提交按钮添加事件响应,将编辑框的内容保存到文件中 submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Editor eidtor=shared.edit(); //声明Editor对象 if(name.getText()!=null && email.getText()!=null) { if(!"".equals(name.getText().toString()) && !"".equals(email.getText().toString())) { eidtor.putString("name", name.getText().toString()); eidtor.putString("email", email.getText().toString()); eidtor.commit(); //编辑完成,保存 showToast("成功添加信息!"); look.performClick(); //调用查看按钮的效果 } } } }); //为清空按钮添加事件监听,清除文件中保存的内容 clear.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Editor eidtor=shared.edit(); //声明Editor对象 eidtor.clear(); eidtor.commit(); showToast("信息清除成功!!!"); look.performClick(); //调用查看按钮的效果 } }); } //清空编辑框的内容 private void clearEdit() { name.setText(""); email.setText(""); } private void showToast(String string) { Toast toast=Toast.makeText(this,string, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}
阅读全文
0 0
- 数据存储:轻量级存储类SharedPreferences的一个应用实例
- SharedPreferences轻量级数据存储
- 【数据存储】sharedPreferences-轻量级存储
- SharedPreferences(轻量级的存储)-->简单应用
- 轻量级数据存储方式--SharedPreferences
- 轻量级SharedPreferences的存储方式
- Android中一个轻量级的存储方案(SharedPreferences)
- Android sharedpreferences轻量级存储
- SharedPreferences轻量级存储
- Android SharedPreferences的轻量级数据存储类的使用 记住密码小案例
- Android数据存储 SharedPreferences实例
- [Android] SharedPreferences(轻量级的存储方式)
- [Android] SharedPreferences(轻量级的存储方式)
- SharedPreferences(偏好设定;轻量级的存储)
- SharedPreferences轻量级存储工具类--方便使用
- 轻量级文件存储SharedPreferences使用管理类
- android应用的本地存储SharedPreferences类
- SharedPreferences数据存储类的简单使用
- Function类型
- 微信小程序的支付和退款流程
- ETH 油费
- Android:ImageView 中ScaleType属性详解
- STM32如何使用内部时钟源当系统时钟
- 数据存储:轻量级存储类SharedPreferences的一个应用实例
- X264编码(2)
- 为什么要写博客?
- spring 学习
- Python中六大排序算法与代码实现
- 线程猜数字
- iOS自己实现二维码生成与扫描
- 周四见|《从50分到0.59秒,SQL优化让不可能变成可能》by松华
- 多线程