2013-05-28 9 views
8

XML 레이아웃에서 비정상 인 사용자 정의 버튼을 만들었습니다. 클릭 리스너가 실행되지 않는 것을 제외하면 모든 것이 올바르게 작동합니다.
android:clickable="true" 속성으로 인해 문제가 발생한 것으로 의심됩니다. 제거 할 때 클릭 리스너가 트리거됩니다. 그러나 사용자 정의보기에서 선택기를 배경으로 사용하므로이 속성을 설정해야합니다. 선택기를 제거하면 선택기가 더 이상 작동하지 않습니다.OnClickListener가 사용자 정의보기로 트리거되지 않았습니다.

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

은 XML 레이아웃 사용자 정의보기에서 팽창 : 여기

클래스 정의의

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

그리고 여기가 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"/> 

및 클릭 액티비티 설정 방법 :

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

이 문제를 이미 해결 한 여러 스레드가 있지만 그 중 일부는 저에게 도움이되지 않았습니다. 어떤 도움을 주시면 감사하겠습니다.

+0

사용자 정의 단추는 LinearLayout을 확장합니다. 그리고 그것을 위해 onClickListener를 설정하려고합니다. 그게 니가 원하는거야.? – SKK

+0

예, 그런 것입니다. 그러나 반면에 CustomButton에는 배경 선택기가 있습니다. –

+0

버튼이 보이지 않습니다. 어디에서나 설정할 수 있습니다 (true). 정상입니까? – wazaminator

답변

11

내가 android:clickable="true"이 선택 배경을 트리거하지 않습니다 설정하지만, View 클래스에서 setOnClickListener() 방법을 살펴 복용 후, 나는 setClickable(true)가 호출되는 것을보고하지 않는 것이 걱정이었다 제거,

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

을 따라서 레이아웃 선언에서 android:clickable="true"을 가져오고 내 맞춤 검색 버튼에 대한 클릭 수신기 만 설정하면 문제가 해결되었습니다.

+0

잘 잡습니다. 그러나 그것이 왜 그렇게 명확한지는 알 수 없습니다. 'View.setOnClickListener()'는 clickable을 true로 설정하고 listener는 어떤 경우에도 설정됩니다. – Levor

관련 문제