0. Result
1. Using PreferenceManager, get the default SharedPreference
Set setTextView() based on the value from the SharedPreference
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | package com.tistory.qlyh8.preferences; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.preference.PreferenceManager; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView textView1, textView2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = findViewById(R.id.text1); textView2 = findViewById(R.id.text2); setupSharedPreferences(); } // text1 세팅 (Shown / Hidden) private void setTextView1(boolean isTrue){ if(isTrue) textView1.setText("Text1"); else textView1.setText(""); } // text2 세팅 (Shown / Hidden) private void setTextView2(boolean isTrue){ if(isTrue) textView2.setText("Text2"); else textView2.setText(""); } // SharedPreferences 에서 모든 값을 가져 와서 설정 private void setupSharedPreferences() { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); // text1 세팅 (Shown / Hidden) setTextView1(sharedPreferences.getBoolean(getString(R.string.pref_show_text1_key), getResources().getBoolean(R.bool.pref_show_text1_default))); // text2 세팅 (Shown / Hidden) setTextView2(sharedPreferences.getBoolean(getString(R.string.pref_show_text2_key), getResources().getBoolean(R.bool.pref_show_text2_default))); /*SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean(getString(R.string.pref_show_text1_key), getResources().getBoolean(R.bool.pref_show_text1_default)); editor.apply();*/ } ... } | cs |
2. Implement OnShardPreferenceChangeListener
Override the onSharedPreferenceChanged method and update the show textView preference
Register the listener
Override onDestory and unregister the listener
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | package com.tistory.qlyh8.preferences; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.preference.PreferenceManager; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener { TextView textView1, textView2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = findViewById(R.id.text1); textView2 = findViewById(R.id.text2); setupSharedPreferences(); } // text1 세팅 (Shown / Hidden) private void setTextView1(boolean isTrue){...} // text2 세팅 (Shown / Hidden) private void setTextView2(boolean isTrue){...} // SharedPreferences 에서 모든 값을 가져 와서 설정 private void setupSharedPreferences() { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); // text1 세팅 (Shown / Hidden) setTextView1(sharedPreferences.getBoolean(getString(R.string.pref_show_text1_key), getResources().getBoolean(R.bool.pref_show_text1_default))); // text2 세팅 (Shown / Hidden) setTextView2(sharedPreferences.getBoolean(getString(R.string.pref_show_text2_key), getResources().getBoolean(R.bool.pref_show_text2_default))); /*SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean(getString(R.string.pref_show_text1_key), getResources().getBoolean(R.bool.pref_show_text1_default)); editor.apply();*/ // 리스너 등록 // 리스너를 implement 해주었기 때문에 this sharedPreferences.registerOnSharedPreferenceChangeListener(this); } // shared preferences 이 변경되면 화면을 업데이트 // OnSharedPreferenceChangedListener 를 구현하는 클래스를 만들 때 이 메서드가 필요 @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if(key.equals(getString(R.string.pref_show_text1_key))) { setTextView1(sharedPreferences.getBoolean(key, getResources().getBoolean(R.bool.pref_show_text1_default))); } else if(key.equals(getString(R.string.pref_show_text2_key))) { setTextView2(sharedPreferences.getBoolean(key, getResources().getBoolean(R.bool.pref_show_text2_default))); } } @Override protected void onDestroy() { super.onDestroy(); //리스너 해지 PreferenceManager.getDefaultSharedPreferences(this) .unregisterOnSharedPreferenceChangeListener(this); } //메뉴 생성 @Override public boolean onCreateOptionsMenu(Menu menu) {...} //메뉴의 항목이 선택되었을 때 호출 @Override public boolean onOptionsItemSelected(MenuItem item) {...} } | cs |
Android Course of Udacity - Lesson 6
'Android' 카테고리의 다른 글
Preferences - (6) EditText Preference (0) | 2018.01.04 |
---|---|
Preferences - (5) List Preference (0) | 2018.01.04 |
Preferences - (3) Preference Fragment 만들기 (0) | 2018.01.03 |
Preferences - (2) Settings 메뉴 틀 만들기 (0) | 2018.01.03 |
Preferences - (1) Data Persistence & Preference Fragment (0) | 2018.01.03 |