首先 getSharedPreferences()方法的得到SharedPreferences對象
調用SharedPreferences的edit()方法得到Editor對象編輯器
在調用Editor對象的方法進行存值,最後記得調用Editor對象的commit()方法提交
<?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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="用戶名" />
<EditText
android:id="@+id/et_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="密碼" />
<EditText
android:id="@+id/et_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="亮度" />
<EditText
android:id="@+id/et_light"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/cb_auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="開機是否啓動" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
>
<Button
android:id="@+id/bt_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="保存" />
<Button
android:id="@+id/bt_show"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="顯示" />
</LinearLayout>
</LinearLayout>
public class MainActivity extends Activity implements OnClickListener{
private SharedPreferences sp;
private EditText et_name;
private EditText et_password;
private EditText et_light;
private CheckBox cb_auto;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}
private void initView() {
et_name = (EditText) findViewById(R.id.et_name);
et_password = (EditText) findViewById(R.id.et_password);
et_light = (EditText) findViewById(R.id.et_light);
cb_auto = (CheckBox) findViewById(R.id.cb_auto);
Button bt_save = (Button) findViewById(R.id.bt_save);
Button bt_show = (Button) findViewById(R.id.bt_show);
bt_save.setOnClickListener(this);
bt_show.setOnClickListener(this);
//得到共享首選項
sp = getSharedPreferences("config", Context.MODE_PRIVATE);
}
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
switch (id) {
case R.id.bt_save:
//獲取值
String name = et_name.getText().toString();
String password = et_password.getText().toString();
int light = Integer.parseInt(et_light.getText().toString());
boolean auto = cb_auto.isChecked();
//保存數據
Editor editor = sp.edit();
editor.putString("name", name);
editor.putString("password", password);
editor.putInt("light", light);
editor.putBoolean("auto", auto);
//提交
editor.commit();
break;
case R.id.bt_show:
name = sp.getString("name", "張三");
password = sp.getString("password", "");
light = sp.getInt("light", 15);
auto = sp.getBoolean("auto", true);
et_name.setText(name);
et_password.setText(password);
et_light.setText(light+"");
cb_auto.setChecked(auto);
break;
default:
break;
}
}
}