2016年8月11日 星期四

How to add a seeting page with a toolbar?

13.How to add a seeting page with a toolbar?
we need :
set name (Edittext Text Preference)
on/off (SwitchPreference)
time?
picture?

have a toolbar:

片段單就 Activity 而言,可為您的應用程式提供更有彈性的架構,無論您建置哪一種 Activity 都一樣。 因此,我們建議您盡可能使用PreferenceFragment 來控制設定的顯示,而不要使用 PreferenceActivity

Change from AppCompatPreferenceActivity, to AppCompatActivity+PreferenceFragmentCompat

how to show the value summary?


how to upload image:



we nee to set
user name
N on/off
notification time
upload profile picture


Step
1
create a Fragment extends PreferenceFragment

2.
create a xml folder under res

3.
create a xml file

4.set the xml file

5.create Preference that use to set the time,if i click it,it show clock,time selector


5.1 create a TimePickerPreference extends DialogPreference

5.2


public class TimePickerPreference extends DialogPreference {


   public TimePickerPreference(Context context, AttributeSet attrs) {
       super(context, attrs);
       setTimePickerDialogUI();
   }

   public TimePickerPreference(Context context, AttributeSet attrs, int defStyleAttr) {
       super(context, attrs, defStyleAttr);
       setTimePickerDialogUI();
   }

   private void setTimePickerDialogUI() {
       setDialogLayoutResource(R.layout.timepicker_dialog);
       setPositiveButtonText(android.R.string.ok);
       setNegativeButtonText(android.R.string.cancel);
       setDialogIcon(null);

   }


5.3 create R.layout.timepicker_dialog


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

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

must have AppTheme,if do not have ,the time picker will not use the app theme.

5.4 save the value


save in onDialogClosed

if(ture)
because when click ok,positiveResultis true

protected void onDialogClosed(boolean positiveResult) {
       super.onDialogClosed(positiveResult);
       if (true) {
           int hour = timePicker.getCurrentHour();
           int minutes = timePicker.getCurrentMinute();
           Toast.makeText(mcontext, hour + ":" + minutes, Toast.LENGTH_SHORT).show();

       }
   }

5.6
set Notification time have the initial value


protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
       if (restorePersistedValue) {
           // Restore existing state

       } else {
           // Set default state from the XML attribute
           String mCurrentValue = (String) defaultValue;
           persistString(mCurrentValue);
       }
       String currentValue = this.getPersistedString(mcontext.getResources().getString(R.string.preference_timePick_defaultvalue));
       //set the lock to show
       hour = timeStringToInt(currentValue)[0];
       minutes = timeStringToInt(currentValue)[1];
       /*timePicker.setCurrentHour(hour);
       timePicker.setCurrentMinute(minutes);*/
   }

注意:您無法使用 defaultValue 作為 getPersisted*() 方法中的預設值,因為當 restorePersistedValue 為 true 時,其值永遠是 null。

explain:
當系統將您的 Preference 新增到畫面時,會呼叫 onSetInitialValue() 以通知您設定是否有持續值。 如果沒有持續值,這個呼叫會提供您預設值。

onSetInitialValue() 方法會傳送布林值 restorePersistedValue,以指出該設定的值是否已經持續。 如果是 true,則您應該呼叫其中一個 Preference 類別的 getPersisted*() 方法以擷取持續值,例如,如果為整數值,可使用 getPersistedInt()。 您通常都會想要擷取持續值,如此您可以正確的更新 UI 以反映之前儲存的值。

如果 restorePersistedValue 是 false,則您應該使用在第二引數中傳送的預設值。


5.7
implement default value

@Override
   protected Object onGetDefaultValue(TypedArray a, int index) {
       return a.getString(index);
   }

Total
TimePickerPreference.java

package pom.poly.com.tabatatimer.Preference;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TimePicker;

import pom.poly.com.tabatatimer.R;

/**
* Created by User on 20/6/2016.
*/
public class TimePickerPreference extends DialogPreference {
   private Context mcontext;
   private int hour = 0;
   private int minutes = 0;
   private String TAG = "TimePickerPreference";


   private TimePicker timePicker;

   public TimePickerPreference(Context context, AttributeSet attrs) {
       super(context, attrs);
       setTimePickerDialogUI();
       mcontext = context;
   }

   public TimePickerPreference(Context context, AttributeSet attrs, int defStyleAttr) {
       super(context, attrs, defStyleAttr);
       setTimePickerDialogUI();
       mcontext = context;
   }

   @Override
   protected void onBindDialogView(View view) {
       timePicker = (TimePicker) view.findViewById(R.id.preferencetimePicker);
       timePicker.setCurrentHour(hour);
       timePicker.setCurrentMinute(minutes);
       super.onBindDialogView(view);
   }

   private void setTimePickerDialogUI() {
       setDialogLayoutResource(R.layout.timepicker_dialog);
       setPositiveButtonText(android.R.string.ok);
       setNegativeButtonText(android.R.string.cancel);
       setDialogIcon(null);

   }

   @Override
   protected void onDialogClosed(boolean positiveResult) {
       super.onDialogClosed(positiveResult);
       if (true) {
           hour = timePicker.getCurrentHour();
           minutes = timePicker.getCurrentMinute();
           String time = hour + ":" + minutes;
           //save the value
           persistString(time);

       }
       Log.d(TAG, "in onDialogClosed");
   }

   @Override
   protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
       if (restorePersistedValue) {
           // Restore existing state

       } else {
           // Set default state from the XML attribute
           String mCurrentValue = (String) defaultValue;
           persistString(mCurrentValue);
       }
       String currentValue = this.getPersistedString(mcontext.getResources().getString(R.string.preference_timePick_defaultvalue));
       //set the lock to show
       hour = timeStringToInt(currentValue)[0];
       minutes = timeStringToInt(currentValue)[1];
       /*timePicker.setCurrentHour(hour);
       timePicker.setCurrentMinute(minutes);*/
   }

   @Override
   protected Object onGetDefaultValue(TypedArray a, int index) {
       return a.getString(index);
   }


   private String timeInttoString(int hour, int minutes) {
       String time = hour + ":" + minutes;
       return time;
   }

   private int[] timeStringToInt(String time) {
       String[] parts = time.split(":");
       int[] intArray = new int[2];
       intArray[0] = Integer.parseInt(parts[0]);
       intArray[1] = Integer.parseInt(parts[1]);
       return intArray;
   }

}


preferences.xml


<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
   <EditTextPreference
       android:defaultValue="@string/preference_name_defaultvalue"
       android:dialogTitle="@string/preference_name_dialogTitle"
       android:key="@string/preference_name_key"
       android:title="@string/preference_name_title" />
   <SwitchPreference
       android:defaultValue="@string/preference_nofe_defaultvalue"
       android:key="@string/preference_nof_key"
       android:title="@string/preference_nof_title" />
   <!--TODO set the time picker default value-->

   <pom.poly.com.tabatatimer.Preference.TimePickerPreference

       android:key="@string/preference_timePick_key"
       android:title="@string/preference_timePick_title" />


</PreferenceScreen>


timepicker_dialog.xml

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

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