2013-05-28 25 views
8

Tôi đã tạo nút tùy chỉnh được tăng cấp từ bố cục XML. Mọi thứ hoạt động tốt, ngoại trừ trình nghe click không được kích hoạt.
Tôi nghi ngờ vấn đề là do thuộc tính android:clickable="true", như khi tôi xóa nó, trình nghe nhấp chuột được kích hoạt. Nhưng tôi cần có thuộc tính này được đặt làm chế độ xem tùy chỉnh của tôi sử dụng công cụ chọn làm nền, nếu tôi xóa nó, thì công cụ chọn sẽ không hoạt động nữa.OnClickListener không được kích hoạt cho Chế độ xem tùy chỉnh

Đây là định nghĩa lớp:

public class CustomButton extends LinearLayout{ 

    public CustomButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.custom_button, this, true); 

     TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomButton, 0, 0); 
     String titleStr = a.getString(R.styleable.CustomButton_title); 
     String subTitleStr = a.getString(R.styleable.CustomButton_subTitle); 
     int iconResId = a.getResourceId(R.styleable.CustomButton_icon, R.drawable.ic_launcher); 
     a.recycle(); 

     TextView title = (TextView)findViewById(R.id.title); 
     title.setText(titleStr); 

     TextView subTitle = (TextView)findViewById(R.id.subTitle); 
     subTitle.setText(subTitleStr); 

     ImageView icon = (ImageView)findViewById(R.id.icon); 
     icon.setImageResource(iconResId); 
    } 
} 

Cách bố trí XML rằng giao diện tùy chỉnh được thổi phồng từ:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:background="@drawable/white_box" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 


    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@drawable/main_button_selector" 
      android:clickable="true" 
      android:gravity="center" 
      android:orientation="horizontal"> 

     <ImageView 
       android:id="@+id/icon" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:contentDescription="icon" 
       android:layout_marginLeft="5dip" 
       /> 

     <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="5dip" 
       android:orientation="vertical"> 

      <TextView 
        android:id="@+id/title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="@color/grey_text" 
        android:textSize="@dimen/mainTextSize" 
        android:textStyle="bold"/> 

      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/subTitle" 
        android:ellipsize="end" 
        android:maxLines="2" 
        android:textColor="@color/grey" 
        android:textSize="@dimen/mainSubTextSize"/> 
     </LinearLayout> 
    </LinearLayout> 
</FrameLayout> 

Và dưới đây là cách tôi sử dụng nó trong bố trí XML:

<com.customviews.CustomButton 
       android:id="@+id/pay" 
       android:layout_weight="1" 
       android:layout_width="0dip" 
       android:layout_height="fill_parent" 
       custom:title="Hello World" 
       custom:subTitle="Subtitle" 
       custom:icon="@drawable/ic_launcher"/> 

và cách hoạt động đặt trình xử lý nhấp chuột:

CustomButton button = (CustomButton) findViewById(R.id.pay); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(MyActivity.this, "Hello World!", Toast.LENGTH_LONG).show(); 

Tôi đã thấy một số chủ đề đã giải quyết vấn đề này nhưng không ai trong số họ đã giúp tôi. Sẽ đánh giá cao sự giúp đỡ nào.

+0

Nút tùy chỉnh của bạn mở rộng LinearLayout. Và bạn đang thử đặt onClickListener cho điều đó. là cái gì mà bạn muốn.? – SKK

+0

Vâng, một cái gì đó như thế. Nhưng trong khi đó, CustomButton có bộ chọn nền. –

+0

Tôi không thể nhìn thấy button.setclickable (true) ở bất kỳ đâu, nó có bình thường không? – wazaminator

Trả lời

11

Tôi đã lo lắng rằng không thiết android:clickable="true" sẽ không kích hoạt nền selector, nhưng sau khi tham gia một cái nhìn tại các phương pháp setOnClickListener() từ lớp View, tôi thấy rằng setClickable(true) được gọi là:

public void setOnClickListener(OnClickListener l) { 
     if (!isClickable()) { 
      setClickable(true); 
     } 
     getListenerInfo().mOnClickListener = l; 
} 

Vì vậy, loại bỏ android:clickable="true" từ bố cục bố cục và thiết lập chỉ người nghe nhấp chuột cho nút tùy chỉnh của tôi đã giải quyết được sự cố.

+0

Bắt tốt. Nhưng không thực sự rõ ràng lý do tại sao nó là như vậy. 'View.setOnClickListener()' bộ có thể nhấp vào đúng và người nghe được đặt trong mọi trường hợp. – Levor

Các vấn đề liên quan