Esempio Preferences

 

Creare un nuovo progetto android chiamato HelloPreferences

 

Creare un layout con tre bottoni, per visualizzare, modificare via user interface e modificare via codice le preferenze

<?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">
 
	<Button android:id="@+id/buttonVisualizza"
		android:text="Visualizza Preferenze"
		android:layout_height="wrap_content" 
		android:layout_width="match_parent"></Button>
 
	<Button android:id="@+id/buttonModifica" 
		android:text="Modifica Preferenze da UI" 
		android:layout_height="wrap_content"
		android:layout_width="match_parent"></Button>
 
	<Button android:id="@+id/buttonCambia" 
		android:text="Modifica Preferenze da codice" 
		android:layout_height="wrap_content"
		android:layout_width="match_parent"></Button>
 
</LinearLayout>

 

Creare un file xml/prefs.xml con due campi, username e password

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
	<PreferenceCategory>
		<EditTextPreference 
			android:summary="Inserisci il tuo username"
			android:key="username" 
			android:title="Nome utente">
		</EditTextPreference>
 
		<EditTextPreference 
			android:summary="Inserisci la tua password"
			android:key="password" 
			android:title="Password">
		</EditTextPreference>
	</PreferenceCategory>
 
</PreferenceScreen>

 

Creare una activity per la modifica delle preferenze e mapparla nel manifest

public class Preferenze extends PreferenceActivity {
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
 
		addPreferencesFromResource(R.xml.prefs);
	}
}

        <activity android:name=".Preferenze"
                  android:label="@string/app_name">
        </activity>

 

Gestire nella activity principale gli eventi sul click dei pulsanti.

public class HelloPreferencesActivity extends Activity {
 
	SharedPreferences preferences;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
		preferences = PreferenceManager.getDefaultSharedPreferences(this);
 
		setContentView(R.layout.main);
		Button visualizza = (Button) findViewById(R.id.buttonVisualizza);
 
		visualizza.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String username = preferences.getString("username", "n/a");
				String password = preferences.getString("password", "n/a");
				Toast.makeText(
						getApplicationContext(),
						"Hai impostato username: " + username + 
						", password: "+ password,
						Toast.LENGTH_LONG
					).show();
			}
		});
 
		Button cambia = (Button) findViewById(R.id.buttonCambia);
		cambia.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Editor edit = preferences.edit();
				String username = preferences.getString("username", "n/a");
 
				StringBuffer buffer = new StringBuffer();
				for (int i = username.length() - 1; i >= 0; i--) {
					buffer.append(username.charAt(i));
				}
				edit.putString("username", buffer.toString());
				edit.commit();
 
				Toast.makeText(getApplicationContext(),
						"Invertito user name.",
						Toast.LENGTH_LONG
					).show();
			}
		});
 
		Button modifica = (Button) findViewById(R.id.buttonModifica);
		modifica.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), Preferenze.class);
				startActivity(intent);
			}
		});
 
    }

AttachmentSize
HelloPreferences.zip51.38 KB