软件参数设置
SharedPreferencesActivity
package org.wp.sharedPreferences;
/**
*
* Android平台给我们提供了一个SharedPreferences类
* 它是一个轻量级的存储类,特别适合用于保存软件配置参数。
* 使用SharedPreferences保存数据,其背后是用xml文件存放数据。
* 文件存放在/data/data/<package name>/shared_prefs目录下。
*
*
* 如果访问其他应用中的Preference,前提条件是:
* 该preference创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE权限
* 首先需要创建其他应用的Context,然后通过Context访问preference
* 访问preference时会在其他应用所在包下的shared_prefs目录找到preference
*
* 如果不通过创建Context访问其他应用的preference
* 可以以读取xml文件方式直接访问其他应用preference对应的xml文件
* 如: File xmlFile = new File("/data/data/<package name>/shared_prefs/wp.xml");
* <package name>应替换成应用的包名
*
*/
// try {
// // 构建其他应用的上下文
// Context context = this.createPackageContext("org.wp.file",
// Context.CONTEXT_IGNORE_SECURITY);
// SharedPreferences sharedPreferences = context.getSharedPreferences(
// "soft", Context.MODE_WORLD_READABLE
// + Context.MODE_WORLD_WRITEABLE);
// String namevalue = sharedPreferences.getString("name", "");
// int agevalue = sharedPreferences.getInt("age", 1);
// Log.i(TAG, "name:" + namevalue + ",age:" + agevalue);
// } catch (Exception e) {
// Log.e(TAG, e.toString());
// }
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SharedPreferencesActivity extends Activity {
private EditText nameText;
private EditText ageText;
private TextView resultView;
private Button setbutton;
private Button showbutton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nameText = (EditText) this.findViewById(R.id.name);
ageText = (EditText) this.findViewById(R.id.age);
resultView = (TextView) this.findViewById(R.id.result);
setbutton = (Button) this.findViewById(R.id.setbutton);
showbutton = (Button) this.findViewById(R.id.showbutton);
setbutton.setOnClickListener(listener);
showbutton.setOnClickListener(listener);
SharedPreferences sharedPreferences = SharedPreferencesActivity.this
.getSharedPreferences("soft", Context.MODE_WORLD_READABLE
+ Context.MODE_WORLD_WRITEABLE);
String namevalue = sharedPreferences.getString("name", "");
int agevalue = sharedPreferences.getInt("age", 1);
nameText.setText(namevalue);
ageText.setText(String.valueOf(agevalue));
}
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Button button = (Button) arg0;
/**
* 因为SharedPreferences背后是使用xml文件保存数据
* getSharedPreferences(name,mode)方法的第一个参数用于指定该文件的名称
* 名称不用带后缀,后缀会由Android自动加上
* 方法的第二个参数指定文件的操作模式,共有四种操作模式
* 如果希望SharedPreferences背后使用的xml文件能被其他应用读和写
* 可以指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE权限
*
* 另外Activity还提供了另一个getPreferences(mode)方法操作SharedPreferences
* 这个方法默认使用当前类不带包名的类名作为文件的名称。
*/
// SharedPreferencesActivity.this.getPreferences(Context.MODE_PRIVATE);
// 保存的xml文件名称为SharedPreferencesActivity
SharedPreferences sharedPreferences = SharedPreferencesActivity.this
.getSharedPreferences("soft", Context.MODE_WORLD_WRITEABLE
+ Context.MODE_WORLD_WRITEABLE);
switch (button.getId()) {
case R.id.setbutton: {
String name = nameText.getText().toString();
String age = ageText.getText().toString();
// 获取编辑器
Editor editor = sharedPreferences.edit();
editor.putString("name", name);
editor.putInt("age", Integer.parseInt(age));
// 提交修改
editor.commit();
Toast.makeText(SharedPreferencesActivity.this, "保存成功",
Toast.LENGTH_SHORT).show();
break;
}
case R.id.showbutton: {
// getString()第二个参数为缺省值,如果preference中不存在该key,将返回缺省值
String namevalue = sharedPreferences.getString("name", "");
int agevalue = sharedPreferences.getInt("age", 1);
resultView.setText("姓名:" + namevalue + ",年龄:" + agevalue);
}
}
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textSize="20px"
android:id="@+id/nameLable"
/>
<EditText
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/nameLable"
android:layout_alignTop="@id/nameLable"
android:layout_marginLeft="10px"
android:id="@+id/name"
/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age"
android:textSize="20px"
android:id="@+id/ageLable"
/>
<EditText
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/ageLable"
android:layout_alignTop="@id/ageLable"
android:layout_marginLeft="10px"
android:id="@+id/age"
/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set"
android:id="@+id/setbutton"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/setbutton"
android:layout_alignTop="@id/setbutton"
android:layout_marginLeft="10px"
android:text="@string/show"
android:id="@+id/showbutton"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20px"
android:id="@+id/result"
/>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, SharedPreferencesActivity!</string>
<string name="app_name">软件参数设置</string>
<string name="name">姓名</string>
<string name="age">年龄</string>
<string name="set">保存参数</string>
<string name="show">显示参数</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.wp.sharedPreferences" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SharedPreferencesActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
分享到:
相关推荐
"Android学习笔记" Android学习笔记是Android开发者的必读书籍,书中涵盖了Android系统架构、Activity、Intent、资源管理等多方面的知识。本笔记对应的学习资源《第一行代码》是Android开发者的入门必读书籍,书中...
Android学习笔记(一)——创建第一个Android项目 Android学习笔记(二)android studio基本控件及布局(实现图片查看器) Android学习笔记(三)android studio中CheckBox自定义样式(更换复选框左侧的勾选图像) ...
了解`ImageView`和`ImageButton`的内部工作原理,可以查看它们的源代码,学习Android视图组件的绘制和事件处理机制。这有助于优化性能,比如了解如何高效地加载和缓存图像。 ### 4. 工具使用 开发过程中,可以借助...
### Android学习笔记 #### 1. Android概述 **1.1 Android的特性** - **应用框架**:Android提供了一个强大的应用框架,使得开发者能够轻松地重用基础组件和服务,简化了应用程序的开发流程。 - **Dalvik虚拟机**...
### Android开发学习笔记 #### Button按钮的实现与交互 在Android开发中,`Button`控件是最常用的用户界面元素之一,用于触发特定的操作或事件。本文档将详细讲解如何创建并自定义一个简单的按钮,并设置点击事件...
【Android学习笔记】 Android平台是谷歌推出的一个开放源代码的移动设备操作系统,它为开发者提供了一个全面的软件包,包括操作系统、中间件和关键应用程序。这个平台的主要目标是促进移动应用的创新和多样性,允许...
Android学习笔记(1)-永远不变的Hello World Google的Android SDK发布也有一段时间了,一直想研究一下却苦于找不到时间。利用这个周未,开始强迫自己再次进入学习状态,原因很简单:我看好开放的gPhone。 SDK的下载...
Android开发学习笔记(整理),整理论坛android学习笔记,较完整的讲解了android的内容。包括:view、activity、service、intent、广播机制、http连接、数据sqllite存储、后台线程、各种layout、偏好、本地文件操作、...
这篇学习笔记主要涵盖了关于布局的一些基本概念,特别是`fill_parent`和`wrap_content`这两种尺寸指定方式,以及如何通过XML布局文件来精确控制组件的位置。 首先,`fill_parent`和`wrap_content`是Android布局中的...
根据给定的信息,我们可以从Java和Android学习笔记中提取出一系列重要的知识点,下面将逐一进行详细解释。 ### Java基础知识 #### 1. 命令行基础操作 - **`javacmd`**: 这个命令是Java命令行工具的一部分,用于...
在Android应用开发中,"碎片"(Fragments)是一个关键的概念,它是在Android 3.0(API级别11)引入的,旨在增强用户界面的灵活性,特别是在平板电脑和其他大屏幕...通过实践和学习,你可以更好地掌握这一强大的工具。
Android学习笔记整理.pdf
Android学习笔记全全整理,是针对想要深入理解并掌握Android开发技术的学习者们的一份宝贵资源。这份笔记涵盖了从基础到高级的多个方面,旨在帮助读者建立起完整的Android知识体系。以下将详细介绍其中可能包含的...
本篇学习笔记主要涵盖了ArcGIS for Android的基础配置和核心组件MapVie的使用。 首先,配置ArcGIS for Android项目需要在`Project`级别的`build.gradle`文件中添加Esri的仓库,确保能获取到所需的库。接着,在`...
Android基础学习笔记主要涵盖了一系列关于Android开发的基本概念和关键组件,以下是这些知识点的详细解析: 1. **Activity**: 是Android应用程序的基本单元,它代表用户在屏幕上看到的一个界面。每个Activity都必须...
目录,整理论坛android学习笔记,较完整的讲解了android的内容。包括:view、activity、service、intent、广播机制、http连接、数据sqllite存储、后台线程、各种layout、偏好、本地文件操作、apdapter等几乎全部内容...