Merge "Add a Builder to to creat a dynamic FooterPreference easily"

This commit is contained in:
TreeHugger Robot
2019-08-09 07:21:34 +00:00
committed by Android (Google) Code Review

View File

@@ -16,12 +16,14 @@
package com.android.settingslib.widget;
import android.annotation.StringRes;
import android.content.Context;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.res.TypedArrayUtils;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
@@ -80,4 +82,59 @@ public class FooterPreference extends Preference {
setKey(KEY_FOOTER);
}
}
/**
* The builder is convenient to creat a dynamic FooterPreference.
*/
public static class Builder {
private Context mContext;
private String mKey;
private CharSequence mTitle;
public Builder(@NonNull Context context) {
mContext = context;
}
/**
* To set the key value of the {@link FooterPreference}.
* @param key The key value.
*/
public Builder setKey(String key) {
mKey = key;
return this;
}
/**
* To set the title of the {@link FooterPreference}.
* @param title The title.
*/
public Builder setTitle(CharSequence title) {
mTitle = title;
return this;
}
/**
* To set the title of the {@link FooterPreference}.
* @param titleResId The resource id of the title.
*/
public Builder setTitle(@StringRes int titleResId) {
mTitle = mContext.getText(titleResId);
return this;
}
/**
* To generate the {@link FooterPreference}.
*/
public FooterPreference build() {
final FooterPreference footerPreference = new FooterPreference(mContext);
footerPreference.setSelectable(false);
if (!TextUtils.isEmpty(mKey)) {
footerPreference.setKey(mKey);
}
if (!TextUtils.isEmpty(mTitle)) {
footerPreference.setTitle(mTitle);
}
return footerPreference;
}
}
}