[英]Android - ImageView On Click
Im trying to trace a click when my image view is clicked, but I cannot seem to pick up the touch, any ideas?当我的图像视图被点击时,我试图追踪点击,但我似乎无法触摸,有什么想法吗?
imgView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Log.v(TAG, " click");
}
});
Images normally aren't clickable, therefore I would suggest that you put the definition图片通常不可点击,因此我建议您将定义放在
android:clickable="true"
android:focusable="true"
in your layout.xml to the imageView.在你的 layout.xml 到 imageView。
If this is not helping, please edit+update your question and show us the part of your layout, where you define the image to have a look at it.如果这没有帮助,请编辑+更新您的问题并向我们展示您的布局部分,您可以在其中定义图像以查看它。
EDIT: Tried it, and your code worked with me too - even with the upper case id name.编辑:尝试过,您的代码也适用于我 - 即使使用大写的 id 名称。 Can you please have a close look at your LogCat?
你能仔细看看你的LogCat吗? Mine sometimes doesn't really update, as long as I don't choose the device again.
我的有时不会真正更新,只要我不再选择设备。
To do so in Eclipse, go to the View "Devices" (or show it first via "Window") and click once on your device / virtual device.要在 Eclipse 中执行此操作,请转到查看“设备”(或首先通过“窗口”显示)并在您的设备/虚拟设备上单击一次。
If you still don't find your log entry in the LogCat-View, try to create a filter (via the green plus and giving it the String you defined with TAG
as "by Log Tag").如果您仍然没有在 LogCat-View 中找到您的日志条目,请尝试创建一个过滤器(通过绿色加号并将您用
TAG
定义的字符串指定为“按日志标签”)。
Have a look at android developers > Using DDMS at the section "Using LogCat"在“使用 LogCat”部分查看android 开发人员 > 使用 DDMS
You forgot to override the onClick method.您忘记覆盖 onClick 方法。 This works for me and should do for you as well :-)
这对我有用,也应该对你有用:-)
imgView.setOnClickListener(new View.OnClickListener() {
//@Override
public void onClick(View v) {
Log.v(TAG, " click");
}
});
Here's to get the X and Y coordinates:这是获取 X 和 Y 坐标的方法:
imgView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// X = event.getX()
// Y = event.getY()
return false;
}
});
1) First make your image view clickable by adding-> android:clickable="true" . 1) 首先通过添加-> android:clickable="true" 使您的图像视图可点击。
2) Then go to your java file and add declare a variable for example-> private static ImageView imgview; 2)然后转到您的java文件并添加声明一个变量,例如-> private static ImageView imgview;
3) Then add this functionality: 3)然后添加这个功能:
public void OnclickButtonListener() {
imgview = findViewById(R.id.imageView3);
imgview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(timetable.this,MapsActivity.class);
startActivity(intent);
}
});
}
and call this function in the constructor.并在构造函数中调用这个函数。 Hopefully this would work.
希望这会奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.