我创建了一个自定义视图并设置了onClick
,但它不起作用。它通过错误IllegalStateException: Could not find a method
而这是工作正常时,我尝试它与默认视图。
<com.package.name.CircularTextView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="2dp"
android:onClick="colorClicked"
android:tag="#ffaacc"
app:solidColor="#ffaacc" />
<TextView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="2dp"
android:onClick="colorClicked"
android:text="adhaksjd asdas kd" />
主要活动
public void colorClicked(View view) {
Log.i(TAG, "color clicked...");
/*fragCameraDrawing = (Drawing) getFragmentManager().findFragmentByTag("cameraDrawing");
fragmentCommunicator = (FragmentCommunicator) fragCameraDrawing;
fragmentCommunicator.passingView(view);*/
}
对于自定义视图,这不起作用,但对于默认视图,它起作用。你能告诉我我做错了什么吗?
最佳答案
在自定义视图中使用setOnclickListener
内部。
这样地。
class CircularTextView extends view implement view.OnClickListener{
//other code
// inside constructor
this.setOnClickListener(this);
public void onClick(View v){
Log.i(TAG, "color clicked...");
}
}
关于android - onclick在自定义 View android中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44900720/