在XML中我android:onClick="deleteRow"
工作正常,但现在我需要以编程方式添加此方法。这是代码:
XML:
<ImageView
android:layout_width="0dip"
android:layout_height="50px"
android:layout_weight="1"
android:src="@drawable/delete"
android:layout_gravity="center_vertical"
android:onClick="deleteRow"
android:layout_column="1" />
deleteRow函数
public void deleteRow(View v){
// get row that is parent of clicked img
View row = (View) v.getParent();
// get rows container
ViewGroup container = ((ViewGroup)row.getParent());
// delete the row and invalidate view so it gets redrawn
container.removeView(row);
container.invalidate();
}
以编程方式添加imageView
// Set params for new deleteimage
TableRow.LayoutParams imgParams = new TableRow.LayoutParams(0, 50, 1);
ImageView deleteAns = new ImageView(this);
deleteAns.setImageResource(R.drawable.delete);
tr.addView(deleteAns, imgParams);
答案 0 :(得分:1)
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// your code
}
});
答案 1 :(得分:0)
deleteAns.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteRow(view);
}});