Chinaunix首页 | 论坛 | 博客
  • 博客访问: 868838
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: Java

2010-12-31 11:32:55

软件参数设置

SharedPreferencesActivity

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package org.wp.sharedPreferences;  
 
/** 
 *  
 * Android平台给我们提供了一个SharedPreferences类 
 * 它是一个轻量级的存储类,特别适合用于保存软件配置参数。 
 * 使用SharedPreferences保存数据,其背后是用xml文件存放数据。 
 * 文件存放在/data/data//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//shared_prefs/wp.xml");  
 * 应替换成应用的包名 
 *  
 */  
 
//  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代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
 version="1.0" encoding="utf-8"?>  
 xmlns:android=""  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
     xmlns:android=""  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        >  
            
            android:layout_width="wrap_content"   
            android:layout_height="wrap_content"   
            android:text="@string/name"  
            android:textSize="20px"  
            android:id="@+id/nameLable"  
        />  
          
            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"  
        />  
    >  
     xmlns:android=""  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        >  
            
            android:layout_width="wrap_content"   
            android:layout_height="wrap_content"   
            android:text="@string/age"  
            android:textSize="20px"  
            android:id="@+id/ageLable"  
        />  
          
            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"  
        />  
    >  
     xmlns:android=""  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        >  
          
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="@string/set"  
            android:id="@+id/setbutton"  
        />  
          
            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"  
        />     
    >  
      
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:textSize="20px"  
        android:id="@+id/result"  
    />  
>

strings.xml

Xml代码

1
2
3
4
5
6
7
8
9
 version="1.0" encoding="utf-8"?>  
>  
     name="hello">Hello World, SharedPreferencesActivity!>  
     name="app_name">软件参数设置>  
     name="name">姓名>  
     name="age">年龄>  
     name="set">保存参数>  
     name="show">显示参数>  
>

AndroidManifest.xml

Xml代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 version="1.0" encoding="utf-8"?>  
 xmlns:android=""  
    package="org.wp.sharedPreferences" android:versionCode="1"  
    android:versionName="1.0">  
     android:icon="@drawable/icon" android:label="@string/app_name">  
         android:name=".SharedPreferencesActivity"  
            android:label="@string/app_name">  
            >  
                 android:name="android.intent.action.MAIN" />  
                 android:name="android.intent.category.LAUNCHER" />  
            >  
        >  
    >  
     android:minSdkVersion="7" />  
>
阅读(1783) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-01-01 19:46:22

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com