2012-12-27 61 views

回答

0

使用OnFocusChangeListener(view)方法&重寫OnFocusChange()方法。

+0

button.setOnTouchListener(新View.OnTouchListener(){ \t \t \t公共布爾onTouch(視圖V,MotionEvent事件){ \t \t \t \t如果(event.getAction()== MotionEvent.ACTION_DOWN) \t \t \t \t \t imageView1.setImageResource(R.drawable.alarm); \t \t \t \t如果(event.getAction()== MotionEvent.ACTION_UP) \t \t \t \t \t imageView1.setImageResource(R.drawable.charging_1); \t \t \t \t return false; \t \t \t} \t \t}); –

0

您可以設置一個OnTouchListenerImageView並在OnTouchListener你可以設置你想在MotionEvent.ACTION_DOWN顯示圖像和MotionEvent.ACTION_UP恢復舊的圖像。

+0

歡迎您使用此解決方案! – jaibatrik

0

您可以創建一個自定義按鈕這樣

public class MButton extends Button { 
    public MButton(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     // TODO Auto-generated constructor stub 
    } 

    public MButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 

    } 

    public MButton(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    // define style 
    /** 
    * Send resource ids for normal, selected, pressed in respective order 
    * 
    * @param mImageIds 
    */ 
    public void setDrawablePressedBg(Integer... mImageIds) { 
     StateListDrawable bg = new StateListDrawable(); 
     Drawable normal = this.getResources().getDrawable(mImageIds[0]); 
     Drawable selected = this.getResources().getDrawable(mImageIds[1]); 
     Drawable pressed = this.getResources().getDrawable(mImageIds[2]); 

     bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed); 
     bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected); 
     bg.addState(View.ENABLED_STATE_SET, normal); 
     bg.addState(View.FOCUSED_STATE_SET, selected); 
     bg.addState(View.EMPTY_STATE_SET, normal); 
     this.setBackgroundDrawable(bg); 
    } 

    // define style 
    public void setBackgroundPressedBg(Integer p1, Integer p2, Integer p3) { 

     StateListDrawable bg = new StateListDrawable(); 
     Drawable normal = this.getResources().getDrawable(p1); 
     Drawable selected = this.getResources().getDrawable(p2); 
     Drawable pressed = this.getResources().getDrawable(p3); 

     bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed); 
     bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected); 
     bg.addState(View.ENABLED_STATE_SET, normal); 
     bg.addState(View.FOCUSED_STATE_SET, selected); 
     bg.addState(View.EMPTY_STATE_SET, normal); 
     this.setBackgroundDrawable(bg); 
    } 
} 

而且你可以在你的onCreate方法使用這樣的

this.book_appointment = (MButton) this._view 
       .findViewById(R.id.btn_book); 

     this.book_appointment.setBackgroundPressedBg(R.drawable.btnnormal, 
       R.drawable.btnsel, R.drawable.btnsel); 

這將是這樣的在你的XML佈局文件

<com.packageName.custom.MButton 
     android:id="@+id/btn_book" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/tv_time1" 
     android:layout_marginTop="20dp" 
     android:background="@drawable/bookapointment" /> 
相關問題