res > 에 xml 폴더 생성후 -> preferences.xml을 생성




main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow>
        <TextView
            android:paddingRight="5px"
            android:text="체크박스:" />

        <TextView android:id="@+id/checkbox" />
    </TableRow>

    <TableRow>
        <TextView
            android:paddingRight="5px"
            android:text="벨소리:" />

        <TextView android:id="@+id/ringtone" />
    </TableRow>

    <TableRow>
        <TextView
            android:paddingRight="5px"
            android:text="체크박스 #2:" />

        <TextView android:id="@+id/checkbox2" />
    </TableRow>

</TableLayout>
preferences.xml
<PreferenceScreen
	xmlns:android="http://schemas.android.com/apk/res/android">
	<PreferenceCategory android:title="Simple Preferences">
		<CheckBoxPreference
			android:key="@string/checkbox"
			android:title="체크박스 환경설정"
			android:summary="체크 상태를 변경합니다"
		/>
		<RingtonePreference
			android:key="@string/ringtone"
			android:title="벨소리 환경설정"
			android:showDefault="true"
			android:showSilent="true"
			android:summary="벨소리를 선택합니다"
		/>
	</PreferenceCategory>
	<PreferenceCategory android:title="상세 화면">
		<PreferenceScreen
			android:key="detail"
			android:title="상세 화면"
			android:summary="별도 페이지에 상세 설정이 있습니다">
			<CheckBoxPreference
				android:key="@string/checkbox2"
				android:title="상세 체크박스"
				android:summary="체크 상태를 변경합니다"
			/>
		</PreferenceScreen>
	</PreferenceCategory>
</PreferenceScreen>


main javafile
package com.commonsware.android.prefs;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class StructuredPrefsDemo extends Activity {
	private static final int EDIT_ID =1; 
	private static final int CLOSE_ID =2;

	private TextView checkbox = null;
	private TextView ringtone = null;
	private TextView checkbox2 = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		checkbox = (TextView)findViewById(R.id.checkbox);
		ringtone = (TextView)findViewById(R.id.ringtone);
		checkbox2 = (TextView)findViewById(R.id.checkbox2);
	}
	@Override
	public void onResume(){
		super.onResume();
		
		//환경설정시 생성된 default 환경설정 파일을 읽어들여 SharedPreference 객체 생성
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

		//환경설정 파일에서 저장된 정보의 타입별로 메소드를 호출해 정보를 추출함
		//getBoolean(key,기본값)
		//				  기본값 : key를 통해서  value를 호출하게 되는데
		//					         설정된 값이 없을때 보여지는 값
		checkbox.setText(new Boolean(prefs.getBoolean("checkbox",false)).toString());
		ringtone.setText(prefs.getString("ringtone","<unset>"));
		checkbox2.setText(new Boolean(prefs.getBoolean("checkbox2",false)).toString());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		menu.add(Menu.NONE, EDIT_ID, Menu.NONE, "환경 설정")
		.setIcon(R.drawable.misc);
		menu.add(Menu.NONE, CLOSE_ID, Menu.NONE, "닫기")
		.setIcon(R.drawable.eject);

		return(super.onCreateOptionsMenu(menu));
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case EDIT_ID:
			startActivity(new Intent(this,EditPreferences.class));
			return true;
		case CLOSE_ID:
			finish();
			return true;
		}
		return(super.onOptionsItemSelected(item));
	}
}
sub javafile
package com.commonsware.android.prefs;

import android.preference.PreferenceActivity;
import android.os.Bundle;

public class EditPreferences extends PreferenceActivity{
	@Override
	public void onCreate(Bundle savedIstanceState){
		super.onCreate(savedIstanceState);
		
		addPreferencesFromResource(R.xml.preferences);
	}
}

메인에는 환경설정값을 가져옵니다 (없을시에는 기본값 지정)

환경설정 진입화면



상세화면 진입화면



+ Recent posts