android sharesprefereces 文件操作

来源:互联网 发布:免费的股票数据接口 编辑:程序博客网 时间:2024/06/14 20:48

sharedpreference 文件 一般用于存放android项目的配置文件信息

布局配置文件:

<?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/name" />    <EditText        android:id="@+id/name"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:inputType="text" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/age" />    <EditText        android:id="@+id/age"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:inputType="number" />    <!-- android:gravity="center"  设置布局文件中的组件布局是居中的 -->    <LinearLayout        android:id="@+id/buttonlayout"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:orientation="horizontal" >        <Button            android:id="@+id/button"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/button" />        <Button            android:id="@+id/resume"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/reset" />    </LinearLayout></LinearLayout>


业务的实现 工程简单 就一个activity

package com.itcast.preferences;import com.itcast.preferences.R;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.Toast;public class MainActivity extends Activity {private EditText nameText;// 声明组件private EditText ageText;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);nameText = (EditText) findViewById(R.id.name);// 获得组件对象ageText = (EditText) findViewById(R.id.age);Button button = (Button) this.findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() { // 添加按钮的监听@Overridepublic void onClick(View v) {String name = nameText.getText().toString();// 获得nameTest组建的值String age = ageText.getText().toString();SharedPreferences preferences = getSharedPreferences("xxx", Context.MODE_WORLD_READABLE);// 不调用该方法,这个文件夹你是不会显示的,获得手机内部data/data/主文件的package值/shared_prefs/目录// 1文件名默认的为xml后缀2文件的读取模式Editor editor = preferences.edit();// 获得编辑器 生成的editor.putString("name", name);// 放入要编辑的内容editor.putInt("age", new Integer(age));editor.commit();// 提交 编辑的内同 写入到文件xxx文件中 生成的文件是xml格式的文件Toast.makeText(MainActivity.this, R.string.success, 1).show(); // toast弹出信息}});Button resumebutton = (Button) this.findViewById(R.id.resume);resumebutton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SharedPreferences preferences = getSharedPreferences("xxx", Context.MODE_PRIVATE);// 读取文件String name = preferences.getString("name", "");// 拿到该文件的name值,第二个参数是默认值,如果这个nme没有值,那么第二个参数就是他的值int age = preferences.getInt("age", 20);// 若果age没值 那么20就是他的值nameText.setText(name);// 赋值给nameText组建 就是nameText显示的文本ageText.setText(String.valueOf(age));}});}}


获取别的项目的sharedpreference文件:

public void testGetOtherSharePregerences() throws Exception {// Context.CONTEXT_IGNORE_SECURITY 上下文忽视安全 CONTEXT_INCLUDE_CODE上下文包括代码Context otherContext = getContext().createPackageContext("cn.itcast.xml", Context.CONTEXT_IGNORE_SECURITY);// 创建别的项目的context,1别项目的包名,主xml文件中package值2,创建模式SharedPreferences preferences = otherContext.getSharedPreferences("xxx", Context.MODE_APPEND);// 1要读取的文件名2文件的操作模式,在读取状态下没用preferences.getString("name", "");}


 


 

原创粉丝点击