2010-12-06 54 views
1

我有一个自定义控件在Android中有文本视图,一些图像等。你如何使整个控件在android中可点击?

我想使整个控件可点击。

我宣布与布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:clickable="true" android:layout_width="fill_parent" android:layout_height="50px" android:layout_gravity="center_vertical"> 

即使它有一个机器人:点击属性设置为true,单击事件永远不会触发。

我做错了什么?

回答

3

在主要活动代码,做这样的事情

public class TestActivity extends Activity implements 
    OnTouchListener, OnLongClickListener, OnKeyListener, 
    OnClickListener, OnFocusChangeListener{ 

CustomUI = (CustomView) findViewById(R.id.custom_ui); 

    CustomUI.addListener(this); 
    CustomUI.setOnClickListener(this); 
    CustomUI.setOnFocusChangeListener(this); 
    CustomUI.setOnLongClickListener(this); 
    CustomUI.setOnTouchListener(this); 
    CustomUI.setOnKeyListener(this); 

}

而且你不能把属性在亲戚的布局,你必须包括您的自定义用户界面是此布局.. 。like like

<RelativeLayout 

    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 

<com.company.some.CustomView 

     android:id="@+id/custom_ui" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     /> 

    </RelativeLayout>