我有一个ImageView,想要实现onClickListener。但是当我点击图片时,没有任何反应。即使Logcat也没有显示任何错误。
以下是我的导入语句:
import android.view.View.OnClickListener;
以下是我的图片布局代码:
<ImageView android:id="@+id/favorite_icon"
android:src="@drawable/small_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right" android:paddingTop="63sp"
android:paddingRight="2sp" />
以下是我在活动中定义OnClickListner的事件处理程序的代码:
ImageView imgFavorite = (ImageView) findViewById(R.id.favorite_icon);
imgFavorite.setClickable(true);
imgFavorite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(SystemSettings.APP_TAG + " : " + HomeActivity.class.getName(), "Entered onClick method");
Toast.makeText(v.getContext(),
"The favorite list would appear on clicking this icon",
Toast.LENGTH_LONG).show();
}
});
我有什么遗漏吗?
getApplicationContext()
。使用WhateverYourActivityIs.this
,将您的活动名称替换为WhateverYourActivityIs
。另外,记录一些东西到 LogCat(例如,Log.w()
)通常比仅依赖于Toast
这种诊断工具更有用。 - CommonsWare