Android之SharedPreference
来源:互联网 发布:开票软件登录密码 编辑:程序博客网 时间:2024/05/29 13:52
// by pbImage// 2012-03-26package com.pbAndroid.SharePref;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class pbSharedPref extends Activity {EditText edit_userName = null;EditText edit_passWord = null;Button button_Save = null;Button button_Open = null;String userName = null;String passWord = null;String get_userName = null;String get_passWord = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); edit_userName = (EditText)findViewById(R.id.Edit_userName); edit_passWord = (EditText)findViewById(R.id.Edit_passWord); button_Save = (Button)findViewById(R.id.Button_Save); button_Open = (Button)findViewById(R.id.Button_Open); OnClickListener saveListener = new View.OnClickListener() { //保存事件监听public void onClick(View v) {// TODO Auto-generated method stubSharedPreferences pbSharePref = getSharedPreferences("pbSharePref", MODE_PRIVATE);Editor pbEditor = pbSharePref.edit();userName = edit_userName.getText().toString();passWord = edit_passWord.getText().toString();pbEditor.putString("save_userName", userName);pbEditor.putString("save_passWord", passWord);pbEditor.commit();Toast.makeText(pbSharedPref.this, "save success!", Toast.LENGTH_SHORT).show();}};OnClickListener openListener = new View.OnClickListener() { //打开事件监听public void onClick(View v) {// TODO Auto-generated method stubSharedPreferences pbSharePref = getSharedPreferences("pbSharePref", MODE_PRIVATE);get_userName = pbSharePref.getString("save_userName", "");get_passWord = pbSharePref.getString("save_passWord", "");Toast.makeText(pbSharedPref.this, "帐号: " + get_userName + " 密码: " + get_passWord , Toast.LENGTH_LONG).show();}};button_Save.setOnClickListener(saveListener);button_Open.setOnClickListener(openListener); }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/label_userName" /> <EditText android:hint="" android:id="@+id/Edit_userName" android:layout_width="match_parent" android:layout_height="wrap_content" android:text=""/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/label_passWord"/> <EditText android:hint="" android:id="@+id/Edit_passWord" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" android:password="true"/> <Button android:id="@+id/Button_Save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/button_Save"/> <Button android:id="@+id/Button_Open" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/button_Open"/></LinearLayout>
//>>>>>>>>>>>>>>>>>>>>>结果>>>>>>>>>>>>>>>>>>>
图1 点击【保存>>】
图2 点击【打开>>】
- Android之SharedPreference
- Android之SharedPreference
- android 之SharedPreference
- android基础之sharedpreference
- 【Android葵花宝典】之SharedPreference
- 【Android之SharedPreference
- Android 之SharedPreference使用
- Android 初学之SharedPreference
- Android使用指南之sharedpreference
- Android学习之Sharedpreference保存
- Android 数据存储之 SharedPreference
- Android自学笔记之SharedPreference
- Android 四大存储之 SharedPreference
- Android数据保存之SharedPreference
- Android数据保存之SharedPreference
- android 数据存储之 SharedPreference
- Android 数据存储之SharedPreference
- android存储方式之sharedpreference
- 为jQuery添加格式化日期函数(插件)
- Java中没有sizeof-------来自java编程思想
- C++ 学习笔记 <2> C++ IO
- 简单自定义拖动条
- 一篇关于多线程同步的好文章
- Android之SharedPreference
- C++传引用数组
- css用一张背景图background-position定位
- 从《愤怒的小鸟》看Google Play存在的问题
- 反射一个类中的所有方法
- 第1章 C:穿越时空的迷雾 下
- 9 Android WIFI 驱动移植
- zookeeper安装
- CMPXCHG8B - 比较并交换 8 字节