기록

androidStudio/java/preference:1.1.1 사용하기 본문

Moblie/Android

androidStudio/java/preference:1.1.1 사용하기

youngyin 2020. 9. 29. 08:00

1. 종속성 추가 

File>Project Structure (또는 gradle에 직접 추가)

2. root_preferences.xml

res>xml>root preferences.xml

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory app:title="계정">
        <Preference
            app:key="image"
            app:title="프로필 이미지 변경" />
        <Preference
            app:key="nickname"
            app:title="닉네임 변경" />
        <Preference
            app:key="logout"
            app:title="로그 아웃" />
        <Preference
            app:key="signout"
            app:title="회원 탈퇴"/>
    </PreferenceCategory>

    <PreferenceCategory app:title="앱정보">
        <Preference
            app:title="app_name"
            app:summary=""/>
        <Preference
            app:title="프로젝트 소개"
            app:summary=""/>
        <Preference
            app:key="opensource"
            app:title="오픈소스 라이선스"/>
        <Preference
            app:key="feedback"
            app:summary="Report technical issues or suggest new features"
            app:title="피드백 보내기"/>
    </PreferenceCategory>

</PreferenceScreen>

3. activity_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:theme="@style/LightTheme">

    <FrameLayout
        android:id="@+id/setting_frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

4. SettingActivity.java

public class SettingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.setting_frameLayout, new SettingsFragment())
                .commit();
    }

    public static class SettingsFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceClickListener {
        private Preference feedback;
        private Preference logout;
        private Preference signout;
        private Preference opensource;
        private Preference image;
        private Preference nickname;

        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
            feedback = findPreference("feedback");
            logout = findPreference("logout");
            signout = findPreference("signout");
            opensource = findPreference("opensource");
            image = findPreference("image");
            nickname = findPreference("nickname");

            feedback.setOnPreferenceClickListener(this);
            logout.setOnPreferenceClickListener(this);
            signout.setOnPreferenceClickListener(this);
            opensource.setOnPreferenceClickListener(this);
            image.setOnPreferenceClickListener(this);
            nickname.setOnPreferenceClickListener(this);
        }
		
        // 클릭 이벤트 지정
        @Override
        public boolean onPreferenceClick(final Preference preference) {
            switch (preference.getKey()) {
                case "feedback":
                    return false;
                case "logout":
                    return false;
                case "signout":
                    return false;
                case "opensource":
                    return false;
                default:
                    return false;
            }
        }
    }
}

실행결과

settingActivity 실행결과

Comments