2016-02-23 90 views
0

我有一個自定義的ImageButton,我想單擊更改它的圖像。到目前爲止,我還沒有能夠獲得按鈕內的onClick方法來執行它的操作。Android自定義按鈕偵聽器沒有被調用

public class FlashButtonView extends ImageButton{ 
private Drawable mFlashOffSrc, mFlashOnSrc, mFlashAutoSrc; 
private Drawable mCurrentFlashMode = mFlashAutoSrc; 

public FlashButtonView(Context context) { 
    super(context); 
} 

public FlashButtonView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context.getTheme().obtainStyledAttributes(attrs, R.styleable.FlashButtonView, 0, 0)); 
} 

public FlashButtonView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(context.getTheme().obtainStyledAttributes(attrs, R.styleable.FlashButtonView, 0, defStyleAttr)); 
} 

private void init(TypedArray typedArray) { 
    try { 
     mFlashAutoSrc = typedArray.getDrawable(R.styleable.FlashButtonView_autoSrcImage); 
     mFlashOnSrc = typedArray.getDrawable(R.styleable.FlashButtonView_onSrcImage); 
     mFlashOffSrc = typedArray.getDrawable(R.styleable.FlashButtonView_offSrcImage); 
    } finally { 
     typedArray.recycle(); 
    } 

    setImageDrawable(mFlashAutoSrc); 
    mCurrentFlashMode = mFlashAutoSrc; 

    setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      if(mCurrentFlashMode == mFlashAutoSrc) { 
       setImageDrawable(mFlashOnSrc); 
       mCurrentFlashMode = mFlashOnSrc; 
      } 
      else if(mCurrentFlashMode == mFlashOnSrc){ 
       setImageDrawable(mFlashOffSrc); 
       mCurrentFlashMode = mFlashOffSrc; 
      } 
      else{ 
       setImageDrawable(mFlashAutoSrc); 
       mCurrentFlashMode = mFlashAutoSrc; 
      } 
     } 
    }); 
} 

}

這是我的XML看起來像:

<!--.___ Flash on/off switcher __.--> 
<blablabla.FlashButtonView 
    custom:autoSrcImage="@drawable/button_autoflash" 
    custom:offSrcImage="@drawable/button_noflash" 
    custom:onSrcImage="@drawable/button_flash" 
    android:id="@+id/flash" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginLeft="@dimen/distance_20dp" 
    android:layout_marginStart="@dimen/distance_20dp" 
    android:layout_marginBottom="@dimen/distance_20dp" 
    android:background="@android:color/transparent" 
    android:clickable="true"/> 

我缺少什麼或者做錯了嗎?

+0

你確定init是否被調用? –

+0

我是,按鈕顯示正確。 onClick方法永遠不會被調用。 –

回答

1

實施onTouchEvent()方法。事情是這樣的:

float touched_x, touched_y; 
boolean touched = false; 

@Override public boolean onTouchEvent(MotionEvent event) { 
    touchCounter++; 
    touched_x = event.getX(); 
    touched_y = event.getY(); 
    int action = event.getAction(); 
    switch (action) { 
     case MotionEvent.ACTION_DOWN: 
      touched = true; 
      break; 
     case MotionEvent.ACTION_MOVE: 
      touched = true; 
      break; 
     case MotionEvent.ACTION_UP: 
      touched = false; 
      break; 
     case MotionEvent.ACTION_CANCEL: 
      touched = false; 
      break; 
     case MotionEvent.ACTION_OUTSIDE: 
      touched = false; 
      break; default: 
    } 
    return true; 
}  
0

一個最小的例子有:

public class CustomButton extends ImageButton { 

    public CustomButton(Context context) { 
     super(context); 
     init(); 
    } 

    public CustomButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(); 
    } 

    private void init() { 
     this.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d(getClass().getSimpleName(), "ButtonPressed"); 
      } 
     }); 
    } 
} 

,他們佈局:

<com.example.bossb.test.CustomButton 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

工作正常,我。

相關問題