I have a Custom Preference displaying a TimePicker dialog when clicked. I want to set the summary for the preference when a value is choosed displaying the chosen time. How could this be done? I don't understand how to use the setSummary(CharSequence) method.
The Custom Preference:
public class TimePreference extends DialogPreference {
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
private TimePicker timePicker;
public TimePreference(Context context, AttributeSet attributes) {
super(context, attributes);
setPersistent(false);
}
@Override
public void onBindDialogView(View view) {
super.onBindDialogView(view);
timePicker = (TimePicker) view.findViewById(R.id.prefTimePicker);
timePicker.setCurrentHour(getSharedPreferences().getInt(getKey() + ".hour", hour));
timePicker.setCurrentMinute(getSharedPreferences().getInt(getKey() + ".minute", minute));
timePicker.setIs24HourView(DateFormat.is24HourFormat(timePicker.getContext()));
}
@Override
protected void onDialogClosed(boolean okToSave) {
super.onDialogClosed(okToSave);
if (okToSave) {
timePicker.clearFocus();
SharedPreferences.Editor editor = getEditor();
editor.putInt(getKey() + ".hour", timePicker.getCurrentHour());
editor.putInt(getKey() + ".minute", timePicker.getCurrentMinute());
editor.commit();
// Here should be the setSummary() method, maybe?
}
}
}
The main activity:
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PrefsFragment()).commit();
}
}
Use following lines in your DialogPreference constructor
setTitle("Your Preference Name goes here");
setSummary("Your summary goes here")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.