2010-12-06 53 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>