Created
July 13, 2016 01:58
-
-
Save alvareztech/0945267d57f9516a19f3c555db07742f to your computer and use it in GitHub Desktop.
Aplicación ejemplo de uso de preferencias. Curso de desarrollo de aplicaciones Android - Sesión 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="tech.alvarez.ejemplopreferencias.MainActivity"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="48dp" | |
android:orientation="horizontal"> | |
<EditText | |
android:id="@+id/nombreEditText" | |
android:layout_width="0dp" | |
android:layout_height="48dp" | |
android:layout_weight="1" | |
android:hint="@string/nombre" | |
android:inputType="text" /> | |
<Button | |
android:id="@+id/GuardarButton" | |
android:layout_width="0dp" | |
android:layout_height="48dp" | |
android:layout_weight="1" | |
android:onClick="guardar" | |
android:text="Guardar" | |
android:textSize="12sp" | |
/> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="48dp" | |
android:orientation="horizontal"> | |
<Switch | |
android:id="@+id/configuracion1Switch" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:layout_weight="1" | |
android:onClick="cambiarConfiguracion1" | |
android:text="@string/configuracion1" /> | |
<Switch | |
android:id="@+id/configuracion2Switch" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:layout_weight="1" | |
android:onClick="cambiarConfiguracion2" | |
android:text="@string/configuracion2" /> | |
</LinearLayout> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez.ejemplopreferencias; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.EditText; | |
import android.widget.Switch; | |
public class MainActivity extends AppCompatActivity { | |
private EditText nombreEditText; | |
private Switch configuracion1Switch; | |
private Switch configuracion2Switch; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
nombreEditText = (EditText) findViewById(R.id.nombreEditText); | |
configuracion1Switch = (Switch) findViewById(R.id.configuracion1Switch); | |
configuracion2Switch = (Switch) findViewById(R.id.configuracion2Switch); | |
// leer | |
SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE); | |
String nombre = preferences.getString("nombre", "Sin nombre"); | |
boolean conf1 = preferences.getBoolean("conf1", false); | |
boolean conf2 = preferences.getBoolean("conf2", false); | |
nombreEditText.setText(nombre); | |
configuracion1Switch.setChecked(conf1); | |
configuracion2Switch.setChecked(conf2); | |
} | |
public void guardar(View view) { | |
String nombre = nombreEditText.getText().toString(); | |
SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE); | |
SharedPreferences.Editor editor = preferences.edit(); | |
editor.putString("nombre", nombre); | |
editor.apply(); | |
} | |
public void cambiarConfiguracion1(View view) { | |
boolean configuracion1 = configuracion1Switch.isChecked(); | |
SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE); | |
SharedPreferences.Editor editor = preferences.edit(); | |
editor.putBoolean("conf1", configuracion1); | |
editor.apply(); | |
} | |
public void cambiarConfiguracion2(View view) { | |
boolean configuracion2 = configuracion2Switch.isChecked(); | |
SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE); | |
SharedPreferences.Editor editor = preferences.edit(); | |
editor.putBoolean("conf2", configuracion2); | |
editor.apply(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment