我一直在以编程方式在 TableLayout 中放置几个 ImageButton,每个 ImageButton 都有自己的 Drawable 资源作为背景。我对 ImageButton 本身的布局使用 XML 描述,然后使用 LayoutInflater 检索这样的 ImageButton(称为genre_cell.xml):
<?xml version="1.0" encoding="utf-8"?>
<ImageButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/genreCellItemId" android:layout_weight="1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="5dip" android:paddingRight="5dip">
</ImageButton>
在我的课堂上,我这样做:
myButton = (ImageButton) inflater.inflate(R.layout.genre_cell, row, false);
我实际上在每个 ImageButton 上都附加了一个 onClickListener,但现在我想唯一标识哪个 ImageButton 已被单击...我想也许我可以以某种方式检索用于背景的 Drawable ID,并使用可用的 Drawable 进行检查整数值?这是一个选项吗?如果是,应该如何实施?还有其他选择吗?