简体   繁体   English

OnClickListener不适用于ImageView

[英]OnClickListener isn't working for ImageView

I'm pretty sure everything is right but the log isn't showing up in the console area of Eclipse when I click any of the images. 我很确定一切都是正确的,但是当我点击任何图像时,日志不会出现在Eclipse的控制台区域中。

Ultimately, I want to have a popup menu appear when these images are clicked but right now I'm just trying to get the onclick events working correctly and then I'll add those in later. 最后,我希望在点击这些图像时出现一个弹出菜单但是现在我只是想让onclick事件正常工作,然后我会在以后添加它们。

My code: 我的代码:

Java Java的

public class SecondScreen extends HelloAndroid {
    public void onCreate(Bundle savedInstanceState) {

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

           super.onCreate(savedInstanceState);
           setContentView(R.layout.main2);

           // initialize header
           ImageView header = (ImageView) findViewById(R.id.header_image);
           header.setOnClickListener(headerClick);

           // Make sure vitals and details are same size
           LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
           params.weight = 0.5f;

           // initialize vitals image
           ImageView vitals = (ImageView) findViewById(R.id.vitalsigns_image);
           vitals.setLayoutParams(params);
           vitals.setOnClickListener(vitalsClick);

           // initialize details image
           ImageView details = (ImageView) findViewById(R.id.details_image);
           details.setLayoutParams(params);
           details.setOnClickListener(detailsClick);




       }

        private OnClickListener headerClick = new OnClickListener() {

            public void onClick(View view) {
                Log.d("Clicked", "Header Clicked!");



            }
       };


        private OnClickListener vitalsClick = new OnClickListener() {

             public void onClick(View view) {
                Log.d("Clicked", "Vitals Clicked!");



             }
        };

        private OnClickListener detailsClick = new OnClickListener() {

             public void onClick(View view) {
                Log.d("Clicked", "Details Click!");



             }
        };
}

XML XML

<?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">

    <!-- HEADER -->

    <ImageView 
       android:id="@+id/header_image"
       android:src="@drawable/header"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
    /> 



<!-- FOOTER -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#000000"  
    android:layout_alignParentBottom="true">
    >


    <ImageView
        android:id="@+id/vitalsigns_image"
        android:src="@drawable/vitalsigns"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_alignParentBottom="true"
    />

    <ImageView
        android:id="@+id/details_image"
        android:src="@drawable/details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_alignParentBottom="true"
    />

</LinearLayout>

What am I doing wrong? 我究竟做错了什么?

I faced the same problem. 我遇到了同样的问题。 After struggling for hours, was able to figure out a solution by myself. 经过几个小时的努力,能够自己找出解决方案。 If the ImageView is inside RelativeLayout or LinearLayout, just add android:focusable="true" . 如果ImageView在RelativeLayout或LinearLayout中,只需添加android:focusable="true" The ImageView also should have android:focusable="true" and android:clickable="true" ImageView也应该有android:focusable="true"android:clickable="true"

I had this issue and fixed in by adding android:layout_width="44dp" to make sure user would have enough area to click on . 我有这个问题并通过添加android:layout_width="44dp"android:layout_width="44dp" ,以确保用户有足够的区域可以点击 Then, you can align image content the way you wannt. 然后,您可以按照您想要的方式对齐图像内容。 In my case: 就我而言:

    <ImageView
    android:id="@+id/locationImageView"
    android:layout_width="44dp"
    android:layout_height="16dp"
    android:layout_centerVertical="true"
    android:layout_marginStart="4dp"
    android:layout_toEndOf="@id/locationText"
    android:adjustViewBounds="true"
    android:scaleType="fitStart"
    android:src="@drawable/icn_edit_small_white"/>

you may need to make your ImageView clickable, either using setClickable(true) or android:clickable="true" in XML 您可能需要使用setClickable(true)android:clickable="true"在XML中使您的ImageView可单击

http://developer.android.com/reference/android/view/View.html#setClickable(boolean ) http://developer.android.com/reference/android/view/View.html#setClickable(boolean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM