1 |
package soo.data.preferencetest; |
---|
2 |
|
---|
3 |
import android.app.Activity; |
---|
4 |
import android.content.Intent; |
---|
5 |
import android.content.SharedPreferences; |
---|
6 |
import android.os.Bundle; |
---|
7 |
import android.view.View; |
---|
8 |
import android.view.View.OnClickListener; |
---|
9 |
import android.widget.Button; |
---|
10 |
import android.widget.CheckBox; |
---|
11 |
import android.widget.EditText; |
---|
12 |
|
---|
13 |
public class PreferenceTest extends Activity { |
---|
14 |
/** Called when the activity is first created. */ |
---|
15 |
|
---|
16 |
@Override |
---|
17 |
public void onCreate(Bundle savedInstanceState) { |
---|
18 |
super.onCreate(savedInstanceState); |
---|
19 |
setContentView(R.layout.main); |
---|
20 |
|
---|
21 |
Button b = (Button)findViewById(R.id.button01); |
---|
22 |
b.setOnClickListener(new OnClickListener() { |
---|
23 |
|
---|
24 |
@Override |
---|
25 |
public void onClick(View v) { |
---|
26 |
// TODO Auto-generated method stub |
---|
27 |
Intent i = new Intent(PreferenceTest.this,SubActivity.class); |
---|
28 |
startActivity(i); |
---|
29 |
} |
---|
30 |
}); |
---|
31 |
init(); |
---|
32 |
} |
---|
33 |
|
---|
34 |
|
---|
35 |
|
---|
36 |
private SharedPreferences sp; |
---|
37 |
private EditText et; |
---|
38 |
private CheckBox cb1,cb2; |
---|
39 |
|
---|
40 |
private void init() { |
---|
41 |
// TODO Auto-generated method stub |
---|
42 |
sp = getSharedPreferences("sp", Activity.MODE_PRIVATE);//�쎄� �곌�媛����. |
---|
43 |
et = (EditText)findViewById(R.id.editText01); |
---|
44 |
cb1 = (CheckBox)findViewById(R.id.checkBox01); |
---|
45 |
cb2 = (CheckBox)findViewById(R.id.checkBox02); |
---|
46 |
} |
---|
47 |
|
---|
48 |
@Override |
---|
49 |
public void onStop(){ //��㈃ ��� : ��㈃���щ�吏���//preference��������.(shared preference) |
---|
50 |
super.onStop(); |
---|
51 |
|
---|
52 |
//1. ��㈃ ���媛������� |
---|
53 |
SharedPreferences.Editor editor = sp.edit(); |
---|
54 |
|
---|
55 |
String value1=et.getText().toString(); |
---|
56 |
boolean value2=cb1.isChecked(); |
---|
57 |
boolean value3=cb2.isChecked(); |
---|
58 |
|
---|
59 |
editor.putString("et_key", value1); |
---|
60 |
editor.putBoolean("cb1_key", value2); |
---|
61 |
editor.putBoolean("cb2_key", value3); |
---|
62 |
|
---|
63 |
editor.commit(); |
---|
64 |
} |
---|
65 |
|
---|
66 |
@Override |
---|
67 |
public void onStart(){ //��㈃��낫瑜�遺����� ��� |
---|
68 |
super.onStart(); |
---|
69 |
|
---|
70 |
//2. �����媛����遺���ㅺ린.. |
---|
71 |
String value1 = sp.getString("et_key",""); |
---|
72 |
boolean value2 = sp.getBoolean("cb1_key", false); |
---|
73 |
boolean value3 = sp.getBoolean("cb2_key", false); |
---|
74 |
|
---|
75 |
et.setText(value1); |
---|
76 |
cb1.setChecked(value2); |
---|
77 |
cb2.setChecked(value3); |
---|
78 |
} |
---|
79 |
} |
---|