프로그래밍/Android2011. 8. 24. 15:01


[main.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
<?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"
    >
 
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity-Private"/>
     
    <EditText android:layout_height="wrap_content"
        android:id="@+id/main_prvate"
        android:layout_width="fill_parent"/>
     
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Shared"/>
     
    <EditText android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/main_shared"/>
     
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/main_saveButton"
        android:text="Save"/>
     
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/main_lauchButton"
        android:text="Launch Activity2"/>
     
 
</LinearLayout>

[activity2.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
<?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"
    >
 
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity-Private"/>
         
    <EditText android:layout_height="wrap_content"
        android:id="@+id/activity2_prvate"
        android:layout_width="fill_parent"/>
     
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Shared"/>
     
    <EditText android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/activity2_shared"/>
         
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/activity2_saveButton"
        android:text="Save"/>
     
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/activity2_closeActivity"
        android:text="Back to Main" android:layout_gravity="right"/>
     
</LinearLayout>

[SharedPreferencesExample.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
public class SharedPreferencesExample extends Activity {
     
    private EditText privateText;
    private EditText sharedText;
    private Button saveButton;
    private Button launchButton;
     
    // 액티비티에서 사용할 값을 저장
    private SharedPreferences prvPref;
     
    // 애플리케이션 전체에서 사용할 값
    private SharedPreferences sharedPref;
     
    private SharedPreferences.Editor prvEditor;
    private SharedPreferences.Editor sharedEditor;
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Initiate Widgets
        privateText = (EditText)findViewById(R.id.main_prvate);
        sharedText = (EditText)findViewById(R.id.main_shared);
        saveButton = (Button)findViewById(R.id.main_saveButton);
        launchButton = (Button)findViewById(R.id.main_lauchButton);
         
        prvPref = getPreferences(Activity.MODE_PRIVATE);
        sharedPref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
         
        prvEditor = prvPref.edit();
        sharedEditor = sharedPref.edit();
         
         
        saveButton.setOnClickListener(new OnClickListener(){
 
            @Override
            public void onClick(View v) {
                String prvValue = privateText.getText().toString();
                String sharedValue = sharedText.getText().toString();
                prvEditor.putString("value", prvValue);
                sharedEditor.putString("value", sharedValue);
                 
                prvEditor.commit();
                sharedEditor.commit();
                Toast.makeText(SharedPreferencesExample.this, "Saved", Toast.LENGTH_SHORT).show();
            }
             
        });
         
        launchButton.setOnClickListener(new OnClickListener(){
 
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SharedPreferencesExample.this, Activity2.class));
                 
            }
             
        });
    }
     
    public void onResume(){
        super.onResume();
        String prvValue = prvPref.getString("value", "");
        String sharedValue = sharedPref.getString("value", "");
         
        privateText.setText(String.valueOf(prvValue));
        sharedText.setText(String.valueOf(sharedValue));
    }
}

[Activity2.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
public class Activity2 extends Activity {
 
    private EditText privateText;
    private EditText sharedText;
    private Button saveButton;
    private Button finishButton;
     
    private SharedPreferences prvPref;
    private SharedPreferences sharedPref;
    private SharedPreferences.Editor prvEditor;
    private SharedPreferences.Editor sharedEditor;
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
         
        // Initiate Widgets
        privateText = (EditText)findViewById(R.id.activity2_prvate);
        sharedText = (EditText)findViewById(R.id.activity2_shared);
        saveButton = (Button)findViewById(R.id.activity2_saveButton);
        finishButton = (Button)findViewById(R.id.activity2_closeActivity);
         
        prvPref = getPreferences(Activity.MODE_PRIVATE);
        sharedPref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
         
        prvEditor = prvPref.edit();
        sharedEditor = sharedPref.edit();
                         
        saveButton.setOnClickListener(new OnClickListener(){
 
            @Override
            public void onClick(View v) {
                String prvValue = privateText.getText().toString();
                String sharedValue = sharedText.getText().toString();
                prvEditor.putString("value", prvValue);
                sharedEditor.putString("value", sharedValue);
                 
                prvEditor.commit();
                sharedEditor.commit();
                Toast.makeText(Activity2.this, "Saved", Toast.LENGTH_SHORT).show();
            }
             
        });
         
        finishButton.setOnClickListener(new OnClickListener(){
 
            @Override
            public void onClick(View v) {
                finish();
                 
            }
             
        });
    }
     
       public void onResume(){
            super.onResume();
            String prvValue = prvPref.getString("value", "");
            String sharedValue = sharedPref.getString("value", "");
             
            privateText.setText(String.valueOf(prvValue));
            sharedText.setText(String.valueOf(sharedValue));
        }
 
}







출처 - 기초부터 다지는 커니의 안드로이드




'프로그래밍 > Android' 카테고리의 다른 글

위치 정보 사용 - 현재 좌표 알기  (0) 2011.08.25
위치 기반 서비스  (1) 2011.08.25
SharedPreferences 설명  (0) 2011.08.24
콘텐트 프로바이더  (0) 2011.08.24
SQLite3 - 데이터베이스 어댑터  (0) 2011.08.23
Posted by 건깡