Android 如何将OnClickListener附加到自定义首选项布局中的按钮?
我有一个首选项屏幕,其中填充了数据库中的项目。我通过创建自己的Android 如何将OnClickListener附加到自定义首选项布局中的按钮?,android,layout,preferences,Android,Layout,Preferences,我有一个首选项屏幕,其中填充了数据库中的项目。我通过创建自己的首选项活动来实现这一点。在“活动”中,我创建了对话框首选项项,并将它们添加到我的首选项类别以在屏幕上设置首选项的样式,我使用自定义布局,并使用setLayoutResource(R.layout.custom\u pref\u行) 这基本上会在布局右侧对齐的视图中添加一个ImageButton。这一切都很好,我的首选项屏幕显示的按钮自定义视图。我的问题是如何将单击侦听器附加到自定义视图中的按钮?我无法从首选项活动中找到行的视图。如果我
首选项活动
来实现这一点。在“活动”中,我创建了对话框首选项
项,并将它们添加到我的首选项类别
以在屏幕上设置首选项的样式,我使用自定义布局,并使用setLayoutResource(R.layout.custom\u pref\u行)
这基本上会在布局右侧对齐的视图中添加一个ImageButton
。这一切都很好,我的首选项屏幕显示的按钮自定义视图。我的问题是如何将单击侦听器附加到自定义视图中的按钮?我无法从首选项活动
中找到行的视图
。如果我的项目不是动态创建的,我可能可以从XML中完成这一切,然后引用id或按钮,但我可以这样做,因为我是动态创建列表的
关于如何获得每个项目的ImageButton
的句柄,有什么建议吗?最后,我想配置按钮以启动删除确认对话框
R.layout.custom_pref_行:
<?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
并覆盖onbindialogview(视图视图)
。在此方法中,您可以执行以下操作:
@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);
}
对话框首选项的子类可以保存与它所表示的项相关的任何状态/值
查看有关扩展对话框首选项的一般指导原则
希望这有帮助 好吧,肖邦让我朝着另一个方向思考。我没有意识到首选项
对象也负责其选择器在首选项屏幕中的显示方式
函数的作用是:设置对话框本身的资源,而不是首选项屏幕中显示的行。这很混乱,我错误地试图在首选项屏幕中使用它来调整选择器布局
解决方案是覆盖onCreateView
并在那里返回自定义布局。对我来说,这是违反直觉的,因为在大多数其他情况下,该方法通常控制最终视图
我总是将我的首选项(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()。
现在唯一缺少的是,当从首选项屏幕单击时,该项不再显示突出显示。你知道我应该应用什么样的样式才能让列表像平常一样显示突出显示吗?因为我已经将DialogPreference子类化了,这应该很容易实现。我试试看。谢谢。这不是一个可行的解决办法。我不想访问对话框首选项
视图上的按钮。我正在试着找到启动对话框的首选项屏幕的按钮。肖邦,你让我朝着正确的方向思考。正确的想法,但错误的功能。我需要重写onCreateView上的onCreateView
。有一些陷阱。我不能再回答我自己的问题4个小时,但我会发布完整的解决方案时,我可以。这正是我要建议的!它必须存在某种事件,你可以忽略它来捕捉通货膨胀过程。我很高兴至少我激发了你的想象力:)
@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;
}