Android--SharedPreference应用
来源:互联网 发布:火狐浏览器 java插件 编辑:程序博客网 时间:2024/05/29 18:13
- 注: 其它应用调用此参数文件:
-
-
-
-
- Context context = this.getContext().createPackageContext("cn.android.preference",this.getContext().CONTEXT_IGNORE_SECURITY);
-
- SharedPreferences preferences = context.getSharedPreferences("user", context.MODE_WORLD_READABLE);
-
- String name = preferences.getString("name","abc");
-
- Log.e("TestSharedPreferenceParser",name);
-
-
-
- -------------------------------------------------------------------------------------------------------------------------------------》
-
- package cn.android.preference;
-
- import android.app.Activity;
- 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;
-
-
- publicclass PreferenceActivityextends Activity {
-
-
- private EditText nameText;
-
- private EditText ageText;
-
-
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
-
- nameText = (EditText) findViewById(R.id.name);
-
- ageText = (EditText) findViewById(R.id.age);
-
-
- Button saveButton = (Button) findViewById(R.id.save);
-
- Button readButton = (Button) findViewById(R.id.read);
-
-
- saveButton.setOnClickListener(new View.OnClickListener() {
-
- @Override
- publicvoid onClick(View v) {
-
-
-
- SharedPreferences preferences = getSharedPreferences("user", PreferenceActivity.MODE_WORLD_READABLE);
-
-
- Editor edit = preferences.edit();
-
- edit.putString("name", nameText.getText().toString());
-
- String ageStr = ageText.getText().toString();
- if(ageStr !=null ||"".equals(ageStr.trim()))
- edit.putInt("age",new Integer(ageStr));
-
- edit.commit();
-
- Toast.makeText(PreferenceActivity.this, R.string.save_success, Toast.LENGTH_LONG).show();
- }
-
- });
-
-
- readButton.setOnClickListener(new View.OnClickListener() {
-
- @Override
- publicvoid onClick(View v) {
-
-
-
- SharedPreferences preferences = getSharedPreferences("user", PreferenceActivity.MODE_PRIVATE);
-
- String name = preferences.getString("name","");
-
- Integer age = preferences.getInt("age",0);
-
- nameText.setText(name);
-
- ageText.setText(age + "");
-
- Toast.makeText(PreferenceActivity.this, R.string.read_success, Toast.LENGTH_LONG).show();
- }
-
- });
- }
- }
-
-
- ------------------------------------------------------------------>
- 页面布局mian.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">
-
- <LinearLayout
- android:orientation="horizontal"
- 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"/>
-
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/name" />
-
- </LinearLayout>
-
- <LinearLayout
- android:orientation="horizontal"
- 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"/>
-
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/age" />
-
- </LinearLayout>
-
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/save"
- android:id="@+id/save" />
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/read"
- android:id="@+id/read" />
-
- </LinearLayout>
-
- </LinearLayout>