【问题标题】:How to attach OnClickListener to a button in a custom preference layout?如何将 OnClickListener 附加到自定义首选项布局中的按钮?
【发布时间】:2012-05-01 21:11:15
【问题描述】:

我有一个首选项屏幕,其中填充了数据库中的项目。我通过创建自己的PreferenceActivity 来完成这项工作。在活动中,我创建DialogPreference 项目并将它们添加到我的PreferenceCategory 为了在屏幕上设置首选项的样式,我使用自定义布局并使用setLayoutResource(R.layout.custom_pref_row) 应用它

这基本上将ImageButton 添加到与布局右侧对齐的视图中。这一切都很好,我的偏好屏幕显示带有按钮的自定义视图。我的问题是如何将点击侦听器附加到自定义视图中的按钮?我无法找到从PreferenceActivity 获取行的View 的方法。如果我的项目不是动态创建的,我也许可以从 XML 中完成这一切,然后引用 id 或按钮,但我可以这样做,因为我正在动态创建列表。

关于如何处理每个项目的ImageButton 有什么建议吗?最后我想配置按钮以启动删除确认对话框。

R.layout.custom_pref_row:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dip"
    android:layout_marginRight="6dip"
    android:layout_marginTop="6dip"
    android:layout_marginBottom="6dip"
    android:layout_weight="1">

    <TextView android:id="@+android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal" />

    <TextView android:id="@+android:id/summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/title"
        android:layout_alignLeft="@android:id/title"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:maxLines="2" />

    <ImageButton android:id="@+id/pref_delete_station" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_trash_can" android:layout_alignParentRight="true" android:background="@null"></ImageButton>

</RelativeLayout>

<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@+android:id/widget_frame"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" />


</LinearLayout>

我的 PreferenceActivity 的相关部分:

DialogPreference diaPref;
for (Station mStation : sList) {
        diaPref = new StationEditor(this.getPreferenceScreen().getContext(), null, this, mStation);
        diaPref.setLayoutResource(R.layout.custom_pref_row);
        diaPref.setTitle(mStation.getName());
        diaPref.setKey(STATION_PREFIX + mStation.getId());

        // add new preference
        stationTypesCategory.addPreference(diaPref);
}

【问题讨论】:

    标签: android layout preferences


    【解决方案1】:

    您可以扩展DialogPreference 并覆盖onBindDialogView(View view)。在这个方法中你可以这样做:

    @Override
    protected void onBindDialogView(View view) {
    
        ((ImageButton) view.findViewById(R.id.pref_delete_station)).setOnClickListener(new OnClickListener() {      
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
            }
        });
    
        super.onBindDialogView(view);
    }
    

    DialogPreference 的子类可以保存与其所代表的项目相关的任何状态/值。

    查看this question 了解扩展DialogPreference 的一般准则。

    希望这会有所帮助!

    【讨论】:

    • 因为我已经继承了 DialogPreference 这应该很容易实现。我会试一试。谢谢。
    • 这不是一个可行的解决方案。我希望访问DialogPreference 视图上的按钮。我正在尝试获取作为启动对话框的首选项屏幕一部分的按钮。
    • 肖邦,你让我想到了正确的方向。想法正确但功能错误。我需要改写onCreateView。有一些问题。再过 4 个小时我无法回答我自己的问题,但我会尽可能发布完整的解决方案。
    • 这正是我要建议的!它必须存在某种可以覆盖的事件以捕捉通货膨胀过程。我很高兴至少激发了你的想象力:)
    【解决方案2】:

    好吧,肖邦让我想到了一个不同的方向。我没有意识到Preference 对象还负责其选择器在首选项屏幕中的显示方式。

    setLayoutResouce() 函数为 Dialog 本身设置资源,而不是在 Preference 屏幕中看到的行。这很令人困惑,我错误地尝试在首选项屏幕中使用它来调整那里的选择器布局。

    解决方案是覆盖onCreateView 并在那里返回自定义布局。对我来说,这是违反直觉的,因为在大多数其他情况下,该方法通常控制最终视图。

    我已经将我的Preference (DialogPreference) 子类化了,所以我所要做的就是添加以下内容...

    @Override
    protected View onCreateView (ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View customRow = inflater.inflate(R.layout.preferences_station_list_row, null);
        ((ImageButton) customRow.findViewById(R.id.pref_delete_station)).setOnClickListener(new OnClickListener() {      
            @Override
            public void onClick(View v) {
               Log.i("c","clicked");
            }
        });
    
        customRow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(null);
            }
        });
        customRow.setClickable(true);
        return customRow;
    }
    

    我遇到的一个问题是,起初行本身不再可点击,但按钮可以。我必须在整个视图上添加一个监听器并手动调用ShowDialog(). 现在唯一缺少的是从“首选项”屏幕单击该项目时不再显示突出显示。知道我应该应用什么样式,以便列表像往常一样显示突出显示吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多