我的侦听器有问题,我使用 ArrayAdapter 创建了一个列表,并使用 WML 为每个项目设置了文本和图像:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/item_macro"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:contentDescription="@string/button_delete"
android:src="@drawable/button_delete" />
</RelativeLayout>
和图像的听众。但是当我单击该项目时,事件正在调用,当我单击不是图像时也是如此。
我的适配器类:
public class ListEditable extends ArrayAdapter<Integer> implements OnClickListener, OnLongClickListener {
private final int rowResourceId;
public ListEditable(Context context, int resource, ArrayList<Integer> macros) {
super(context, resource, macros);
this.rowResourceId = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = (View) inflater.inflate(rowResourceId, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.item_macro);
ImageView imageView = (ImageView) rowView.findViewById(R.id.delete);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("toast");
}
});
textView.setText(this.getItem(position));
return rowView;
}
我还有另一个问题,也许是出于同样的原因,当我点击我的图像时,我没有图像焦点,但是我有:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/button_delete_focus" android:state_pressed="true"/>
<item android:drawable="@drawable/button_delete_focus" android:state_focused="true"/>
<item android:drawable="@drawable/button_delete"/>
</selector>
提前感谢您的回答,对不起我的英语不好:)