2013-05-28 42 views
8

Utworzono przycisk niestandardowy, nadmuchany z układu XML. Wszystko działa dobrze, z tym że detektor kliknięcia nie jest uruchamiany.
Podejrzewam, że problem wynika z atrybutu android:clickable="true", ponieważ po jego usunięciu następuje uruchomienie detektora kliknięcia. Ale muszę ustawić ten atrybut, ponieważ mój widok niestandardowy używa selektora jako tła, jeśli go usunę, selektor przestanie działać.Nie można wyzwolić OnClickListener dla widoku niestandardowego

Oto definicja klasy:

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); 
    } 
} 

Układ XML, że widok niestandardowy jest zawyżona z:

<?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> 

A oto jak go używać w układach 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"/> 

oraz sposób, w jaki aktywność ustawia detektor kliknięcia:

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(); 

Widziałem już kilka wątków zajmujących się tym problemem, ale żaden z nich nie pomógł mi. Doceniam każdą pomoc.

+0

Twój CustomButton rozszerza LinearLayout. I Próbujesz ustawić dla tego onClickListener. czy to jest coś, czego chcesz? – SKK

+0

Tak, coś w tym stylu. Ale w międzyczasie CustomButton ma selektor tła. –

+0

Nie widzę przycisku button.setclickable (true) w dowolnym miejscu, czy to normalne? – wazaminator

Odpowiedz

11

Martwiłem się, że nie ustawienie android:clickable="true" nie wywoła tło wyboru, ale po przyjrzeniu się metodą setOnClickListener() z klasy View, widziałem, że setClickable(true) nazywa się:

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

Tak, usuwając android:clickable="true" z deklaracji układu i ustawienie tylko detektor kliknięcia dla mojego przycisku niestandardowego rozwiązało problem.

+0

Dobry połów. Ale nie bardzo jasne, dlaczego tak jest. 'View.setOnClickListener()' ustawia klikalność na true, a detektor jest ustawiany w każdym przypadku. – Levor

Powiązane problemy