Communauté française android news, news du Web et bien d'autres domaines

telephones, smart phones android, programmation et applications, news twitter facebook

Travailler avec les preferences sous ANDROID

By • nov 15th, 2009 • Category: Tutorials
public class simplePrefrence extends Activity implements CompoundButton.OnCheckedChangeListener {
 /** Called when the activity is first created. */
 
 CheckBox chk;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 
 
 Button btn = ( Button) findViewById(R.id.button);
 
 chk = (CheckBox) findViewById(R.id.checkbox);
 
 chk.setOnCheckedChangeListener(this);
 
 btn.setOnClickListener(new Button.OnClickListener(){
 public void onClick(View v){
 finish();
 }
 });
 
 }
 
 
 
 /**called when Checkbox is checked */
 public void onCheckedChanged(CompoundButton cmp, boolean checked){
 if(checked){
 chk.setText("checked");
 }
 else{
 chk.setText("unchecked");         
 }
 
 }
 
 
 public void onResume(){
 super.onResume();
 SharedPreferences  setting = getPreferences(0);
 chk.setChecked( setting.getBoolean("chk", false));
 }
 
 public void onPause(){
 super.onPause();
 
 SharedPreferences  setting = getPreferences(0);
 SharedPreferences.Editor  editor = setting.edit();
 editor.putBoolean("chk", chk.isChecked());
 editor.commit();
 }
 
 
 
}

A voir aussi:

  1. Suite projet Erathquake : les preferences
  2. Earthquake suite du projet android
  3. Services sous android Leçon 1

is
Email this author | All posts by

Comments are closed.