본문 바로가기
모바일/Android

[Android] Preference

by Remover 2012. 7. 4.
반응형

preference.xml

 <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:key="setting_activity"
        android:title="@string/pc1">
    <CheckBoxPreference 
        android:key="remain"
        android:title="@string/checkbox1"
        android:id="@+id/checkbox1"
        android:defaultValue="true"/>

	<CheckBoxPreference
	    android:key="temperature"
	    android:title="@string/checkbox2"
	    android:id="@+id/checkbox2"
	    android:defaultValue="true"/>
	
	<CheckBoxPreference 
	    android:key="time"
	    android:title="@string/checkbox3"
	    android:id="@+id/checkbox3"
	    android:defaultValue="true"/>
	</PreferenceCategory>
	
    <PreferenceCategory
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:key="setting_icon"
        android:title="@string/pc2">
        
        <Preference 
            android:key="setting_icon1"
            android:title="@string/prefer1"
            android:id="@+id/prefer1"/>
        
        <Preference
            android:key="setting_icon2"
            android:title="@string/prefer2"
            android:id="@+id/prefer2"/>
    </PreferenceCategory>
</PreferenceScreen>


MainActivity.java

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends PreferenceActivity implements OnPreferenceClickListener{
	CheckBoxPreference checkbox1, checkbox2, checkbox3;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference);
        Preference pre1=(Preference)findPreference("setting_icon1");
        Preference pre2=(Preference)findPreference("setting_icon2");
        checkbox1 = (CheckBoxPreference)findPreference("remain");
        checkbox2 = (CheckBoxPreference)findPreference("temperature");
        checkbox3 = (CheckBoxPreference)findPreference("time");
        
        //체크인지 아닌지 판단할수 있는 메소드
        //checkbox1.getPreferenceManager().getSharedPreferences().getBoolean("remain", true)

        pre1.setOnPreferenceClickListener(this);
        pre2.setOnPreferenceClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

	public boolean onPreferenceClick(Preference preference) {
		// TODO Auto-generated method stub
		
		if(preference.getKey().equals("setting_icon1"))
			Toast.makeText(MainActivity.this, "기본 아이콘 설정", Toast.LENGTH_SHORT).show();
		
		else if(preference.getKey().equals("setting_icon2"))
			Toast.makeText(MainActivity.this, "나만의 아이콘 설정", Toast.LENGTH_SHORT).show();
		
		return false;
		
	}
}


'모바일 > Android' 카테고리의 다른 글

[Android] 이벤트의 여러가지 방법 2  (0) 2012.07.09
[Android] 이벤트의 여러가지 방법 1  (0) 2012.07.09
[Android] Notification  (0) 2012.07.05
[Android] MediaPlayer  (0) 2012.07.01
[Android] 진동 Vibrator  (0) 2012.07.01

댓글