2012-07-27 102 views
0

我试图创建一个自定义视图组件,如下所示: 组件将作为菜单工作 - 它有4个图像视图,其中会对onClick事件做出反应。 我为它们制作了一个单独的.xml文件,并为它制作了一个类。比我从.xml设置父节点类“class ='myclass'”。 在我的.xml中,一个imageview声明了一个onClick,但是当我尝试使用myclass处理它时 - 应用程序崩溃。哪里不对?或者我想主要问题是:什么是为自定义视图设置事件处理程序的正确方法?处于此视图类定义中的处理程序。我应该写的景色像 创建Android自定义视图

加成的.xml:我怎么,然后,可以从该活动自定义视图将举行它来处理触发事件?

这里是我的菜单的.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
class="com.example.mtgesturestest.MTMenuViewController" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/main_layout" > 

<RelativeLayout 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

     <ImageView 
      android:id="@+id/imageChannels" 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:src="@drawable/channels" 
      android:layout_marginBottom="10dp"   /> 
     <ImageView 
      android:id="@+id/imageMessages" 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:src="@drawable/messages" 
      android:layout_marginBottom="10dp" /> 
     <ImageView 
      android:id="@+id/imageTimeline" 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:src="@drawable/timeline" 
      android:onClick="yell" 
      android:layout_marginBottom="10dp" /> 

</RelativeLayout> 
     <ImageView 
      android:id="@+id/imageMenu" 
      android:layout_width="70dp" 
      android:layout_height="70dp" 
      android:src="@drawable/menu" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginBottom="-15dp" 
      android:onClick="maximizeMenu"/> 

</RelativeLayout> 

应用说它找不到的onClick处理器(maximizeMenu),但其搜索它的活动,我加入这个观点,而不是com.mtgesturestest.MTMenuViewController类。我希望这个菜单是独立的。它应该处理点击,播放一些动画,然后触发父母活动的事件。所以你可以说这是onClick这里是我的自定义视图的内部使用。

+0

显示XML代码到目前为止请 – 2012-07-27 08:34:42

+0

不客气。看原来的帖子 – Graykos 2012-07-27 08:45:37

回答

2

为什么不尝试从MTMenuViewController类中添加的onClick听众喜欢

ImageView menu = (ImageView)this.findViewById(R.id.imageMenu); 
menu.setOnClickListener(PUT THE LISTENER HERE);