Android Ontouchlistener在';s咔嗒一声

Android Ontouchlistener在';s咔嗒一声,android,android-fragments,imageview,ontouchlistener,Android,Android Fragments,Imageview,Ontouchlistener,我在android应用程序的布局中有一个包含6个ImageView的片段。当我点击图像时,它应该会告诉我imageview的id。但是,当我点击任何一个ImageView时,它被认为是错误的。我的图像是三角形的,为了检查我是否点击了图像的内部,我在我的项目中使用了touch listener。但当我点击它时,它并没有显示imageview的id。我在我的项目中使用了一个导航抽屉,所以我在这里使用片段而不是活动。这是我的密码: HomeFragment.java(已编辑) fragment_hom

我在android应用程序的布局中有一个包含6个ImageView的片段。当我点击图像时,它应该会告诉我imageview的id。但是,当我点击任何一个ImageView时,它被认为是错误的。我的图像是三角形的,为了检查我是否点击了图像的内部,我在我的项目中使用了touch listener。但当我点击它时,它并没有显示imageview的id。我在我的项目中使用了一个导航抽屉,所以我在这里使用片段而不是活动。这是我的密码:

HomeFragment.java(已编辑)

fragment_home.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/supportbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="31dp"
        android:background="@drawable/support" />

    <ImageView
        android:id="@+id/flightbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_above="@+id/supportbutton"
        android:layout_alignLeft="@+id/supportbutton"
        android:layout_marginBottom="17dp"
        android:background="@drawable/flight3" />

    <ImageView
        android:id="@+id/hotelbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignBaseline="@+id/flightbutton"
        android:layout_alignBottom="@+id/flightbutton"
        android:layout_centerHorizontal="true"
        android:background="@drawable/hotel2" />

    <ImageView
        android:id="@+id/aboutusbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/hotelbutton"
        android:layout_below="@+id/flightbutton"
        android:background="@drawable/aboutus" />

    <ImageView
        android:id="@+id/loginbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignBaseline="@+id/hotelbutton"
        android:layout_alignBottom="@+id/hotelbutton"
        android:layout_alignParentRight="true"
        android:layout_marginRight="37dp"
        android:background="@drawable/login" />

    <ImageView
        android:id="@+id/contactbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/loginbutton"
        android:layout_centerVertical="true"
        android:background="@drawable/contact"
        android:fitsSystemWindows="false" />

</RelativeLayout>

如果我错了,请纠正我

编辑: 实际上,我的目的是检查实际单击了哪个按钮。问题是,当我添加编辑的部分并运行应用程序时,有趣的是,当我单击联系人图像时,实际上toast显示为登录按钮。但是我在else条件中给出了login-imageview的id,因此当我单击登录图像时,它应该触发toast。单击联系人图像时,实际上会调用toast for login。这怎么可能?是否有任何解决方案…

从的文档中,有两种
makeText()
方法:

  • 公共静态Toast makeText(上下文上下文、int resId、int duration)
  • 公共静态Toast makeText(上下文上下文、字符序列文本、int持续时间)
我假设您希望将ID显示为字符串,但在本例中,您传递的是返回int的
v.getId()
,因此实际上调用了第一个方法,它查找资源。因此,首先你可以成功
Toast.makeText(getActivity(),v.getId()+“”,Toast.LENGTH\u SHORT).show()

这将把您的第二个参数转换为字符串。

您是否调试了
onTouch
上的代码,以查看
颜色返回了什么?您还可以调整颜色值。这取决于您使用的IDE,但在if(color==color.TRANSPARENT)
行上放置一个断点,并在调试模式下运行应用程序。当你点击图像时,应用程序应该在该点暂停,让你检查
颜色
变量。您也可以将颜色值写入日志(
log.d(“您的标签,颜色”);
),或者编写一个
Toast
来显示您的颜色值。@CodeMonkey为什么需要知道
color
的值是什么?从描述中,我了解到显示了一条toast消息,这意味着
颜色不透明。@TeeJay好吧,您的颜色结果明显返回为!=所以第一步(在我看来)是找出从
Color=bmp.getPixel((int)event.getX(),(int)event.getY()返回的实际内容
@CodeMonkey,查看我的编辑。。。请帮帮我…事实上,当我点击联系人图片时,登录就被烤了。当我点击登录图片时,联系人被烤了…这是怎么回事???@TeeJay我在这里:)你在这里和评论中尝试过我的建议了吗?结果是什么?事实上,当我点击联系人图片时,登录被烤了。当我点击登录图像时,联系人被烤了…这是怎么发生的?你能试着删除联系人图像,然后按登录进行测试,看看会烤什么吗?从提供的代码中,我不知道这种行为的原因是什么。如果您提供完整的代码,我可以在我这边测试并检查问题所在。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/supportbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="31dp"
        android:background="@drawable/support" />

    <ImageView
        android:id="@+id/flightbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_above="@+id/supportbutton"
        android:layout_alignLeft="@+id/supportbutton"
        android:layout_marginBottom="17dp"
        android:background="@drawable/flight3" />

    <ImageView
        android:id="@+id/hotelbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignBaseline="@+id/flightbutton"
        android:layout_alignBottom="@+id/flightbutton"
        android:layout_centerHorizontal="true"
        android:background="@drawable/hotel2" />

    <ImageView
        android:id="@+id/aboutusbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/hotelbutton"
        android:layout_below="@+id/flightbutton"
        android:background="@drawable/aboutus" />

    <ImageView
        android:id="@+id/loginbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignBaseline="@+id/hotelbutton"
        android:layout_alignBottom="@+id/hotelbutton"
        android:layout_alignParentRight="true"
        android:layout_marginRight="37dp"
        android:background="@drawable/login" />

    <ImageView
        android:id="@+id/contactbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/loginbutton"
        android:layout_centerVertical="true"
        android:background="@drawable/contact"
        android:fitsSystemWindows="false" />

</RelativeLayout>