[英]How to attach OnClickListener to a button in a custom preference layout?
我有一个首选项屏幕,其中填充了数据库中的项目。 我通过创建自己的PreferenceActivity
这一点。 在活动中我创建DialogPreference
项并将它们添加到我的PreferenceCategory
要在屏幕上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);
}
您可以扩展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);
}
你的sublcass DialogPreference
可容纳与它代表的项目的任何状态/值。
请查看有关扩展DialogPreference
一般准则的问题 。
希望这可以帮助!
好吧,肖邦让我想到了一个不同的方向。 我没有意识到Preference
对象也负责其选择器如何出现在Preference屏幕中。
setLayoutResouce()函数为Dialog本身设置资源,而不是在Preference屏幕中看到的行。 这很令人困惑,我错误地尝试在首选项屏幕中使用它来调整那里的选择器布局。
解决方案是覆盖onCreateView
并在那里返回自定义布局。 对我来说这是违反直觉的,因为该方法通常在大多数其他情况下控制最终视图。
我alraedy子类我的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().
现在唯一缺少的是,从“首选项”屏幕单击时,该项目不再显示突出显示。 知道我应该应用哪些样式,所以列表显示像往常一样的亮点?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.