修改后的ListPreference不会调用其对话框

时间:2012-09-14 18:06:08

标签: android dialog listpreference

我有一个使用自定义ListPreference和CheckBoxPreference制作的PreferenceScreen。这是名为'pref_screen_custom.xml'的XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- My custom preference type.  This just replaces the actual widget
     portion of the preference, if the whole preference wanted to be
     replaced we would use the layout attribute instead of the widgetLayout
     attribute. -->
<com.pref_002.Pref002
        android:key="my_preference"
        android:title="Title: Pref002"
        android:summary="Summary: summary"
        android:defaultValue="-1"
        android:entries="@array/pref002_list_ent"
        android:entryValues="@array/pref002_list_vals"
        android:dialogTitle="title" />

<CheckBoxPreference
        android:key="advanced_checkbox_preference"
        android:title="Title: checkbox preference"
        android:summaryOn="On checkbox" 
        android:summaryOff="Off checkBox" />
</PreferenceScreen>

保存首选项数组的XML是(array.xml):

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="pref002_list_ent">
        <item>T</item>
        <item>V</item>
        <item>S</item>
    </string-array>
    <string-array name="pref002_list_vals">
        <item>10</item>
        <item>8</item>
        <item>6</item>
    </string-array>

</resources>

名为PrefCustom002Activity.java的PreferenceScreen的.java是:

package com.pref_002;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class PrefCustom002Activity extends PreferenceActivity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_screen_custom_001);
    }
}

扩展ListPreference的类叫做'Prefs002.java',它是:

package com.pref_002;

import android.content.Context;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class Pref002 extends ListPreference
{
    // Constructor called by the inflater
    public Pref002(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
    @Override
    protected View onCreateView(ViewGroup parent)
    {
        // New Layout that will contain the default loaded View from ListPreference
        // plus a new one (a Button)
        LinearLayout newLayoutParent = new LinearLayout(getContext());
        newLayoutParent.setOrientation(LinearLayout.HORIZONTAL);
        newLayoutParent.setWeightSum(10.0f);

        // get the View returned from ListPreference 
        View listPreferenceDefaultView = super.onCreateView(parent);

        // new layout values for this View
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        params1.weight = 9.0f;

        listPreferenceDefaultView.setLayoutParams(params1);

        //--Button to be added to newLayoutParent --// 

        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        params2.gravity = Gravity.CENTER_VERTICAL;
        params2.weight = 1.0f;

        Button b = new Button(getContext());
        b.setText("A button");
        b.setLayoutParams(params2);


        // add the two views
        newLayoutParent.addView(listPreferenceDefaultView);
        newLayoutParent.addView(b);
        newLayoutParent.setId(android.R.id.widget_frame);

        return newLayoutParent;
    }
}

所以,正如你在'Prefs002.java'中看到的那样,我重写onCreateView,为ListLreout创建一个新的Layout,它由一个LinearLayout组成,其中包含ListPreference自己生成的View加上一个新的Button,放置在ListPreference生成的视图的右侧。 图形上这是有效的,但是如果我点击这个新视图,则不会显示偏好的对话框(当你点击它时,行也不会变成黄色,但我可以通过添加TouchListener来解决这个问题)。 另外,根据文档,如果我重写OnCreateView,我应该将'widget_frame'设置为新视图,我在行newLayoutParent.setId(android.R.id.widget_frame);中完成了 但是当我点击此视图时,这不会显示对话框。

那么,我如何调用此自定义ListPrerence的对话框?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以通过调用showDialog()(DialogPreference.showDialog)来触发对话框。当您调用super.onCreateView时,返回的视图将具有ID widget_frame的视图。

我会建议这样的事情:

View v = super.onCreateView(parent);
View widgetFrame = v.findViewById(android.R.id.widget_frame);
// add/remove stuff into widgetFrame here
return v;