Show extra info for Preference screens when CheckboxPreference summary field is not enough long?

最后都变了- 提交于 2019-12-21 03:00:34

问题


I have a screen where you can enable/disable modules for my Android application.

For this I use a CheckboxPreference screen. This is all good, but the summary field gets cut off if longer descriptions are added than 2 lines.

Suppose I have 4-5 lines of description available for each module, I would like to display this in a helper window.

I tried to bind a click event to the CheckboxPreference, but that fires for the whole line, so not only when the checkbox is clicked, and more, wherever you click on the line the checkbox is toggled.

So now I am wondering if this can be fixed. So if the user needs more info, just taps the text and the helper opens up, and if want to toggle the settings it taps the checkbox.

How would you do it? I am open to other ideas too, if they do work.


回答1:


Hmn had the same problem today. My alternative solution is to just allow to display more Text within the summary.

Just create your own subclass of CheckBoxPreference that looks something like this:

public class CheckBoxPreferenceWithLongSummary extends CheckBoxPreference{

    public CheckBoxPreferenceWithLongSummary(Context context) {
        super(context);
    }

    public CheckBoxPreferenceWithLongSummary(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CheckBoxPreferenceWithLongSummary(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
        summaryView.setMaxLines(10);
    }
}

Then within your PreferenceScreen (i assume you use xml to set up the Preference layout - not sure if its even possible completely programmatically) just replace the old <CheckBoxPreference.../> with your new implementation like <com.example.whatever.CheckBoxPreferenceWithLongSummary ... />

Seems to work just fine for me, altough i'm not sure about the findViewById(android.R.id.summary) because in the parentClass com.android.internal.R.id.summary is used which seems to be not directly accessible from within Java? Hope its not just an coincidence :)




回答2:


You might be able to create your own subclass of CheckboxPreference that gives you more room.




回答3:


I found an alternative solution that fits better in my case since I needed longer summaries for different types of preferences, not only CheckboxPreferences.

  • I copied the file data/res/layout/preference.xml from the SDK to res/layout/preference_long_summary.xml in my app. (I took the one from API level 7, since it seems to work also for higher levels)
  • removed the android:maxLines attribute from the @+android:id/summary TextView
  • referenced this layout from my preferences for the preferences where I needed longer summary with android:layout="@layout/preference_long_summary"


来源:https://stackoverflow.com/questions/2408708/show-extra-info-for-preference-screens-when-checkboxpreference-summary-field-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!