如何在自定义首选项布局中将OnClickListener附加到按钮?

4

我有一个偏好设置屏幕,其中的项目是从数据库中获取的。为此,我创建了自己的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>

我喜欢的活动相关部分:

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);
}
2个回答

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 子类可以保存与其表示的项目相关的任何状态/值。

请查看关于扩展 DialogPreference 的一般指南的 这个问题

希望这有所帮助!


既然我已经子类化了DialogPreference,这应该很容易实现。我会尝试一下。谢谢。 - Scott Naef
这不是一个可行的解决方案。我不是想要访问DialogPreference视图上的按钮。我试图获取作为首选项屏幕一部分的按钮,该按钮启动对话框。 - Scott Naef
Chopin,你让我朝着正确的方向思考了。想法对了,但函数错了。我需要重写onCreateView。有一些小问题。我还需要等4个小时才能回答自己的问题,但我会在可以的时候发布完整的解决方案。 - Scott Naef
1
这正是我要建议的!一定存在某种事件可以重写以捕获通胀过程。我很高兴至少我激发了你的想象力 :) - Chopin

1

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

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

解决方案是重写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()。现在唯一缺少的是,从首选项屏幕点击后,该项目不再显示高亮。您有什么想法应该应用哪些样式,以便列表像通常一样显示高亮?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接