2012-12-27 2 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 imageView1.setImageResource (R.drawable.charging_1); \t \t \t \t false; \t \t \t} \t \t}); –

0

XML 선택기를 사용해야합니다. 원하는 상태에 관계없이 원하는 드로어 블을 지정할 수 있습니다.

워드 프로세서 현재 위치 : 당신이보기에 그릴 수를 할당 할 때 당신은 당신이에 스타일을 적용 할 각 상태에 대한 참조가있는 XML을 생성합니다

http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html는, 다음 버튼 (참조합니다 예를 들어, 특정 drawable 대신에). 이 SO 자세한 내용은 질문

확인 : Android selector & text color

0

당신은 ImageViewOnTouchListener을 설정할 수 있으며, 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); 
    } 
} 

같은 사용자 정의 버튼 클래스를 만들 수 있습니다 그리고 당신이 당신의 XML 레이아웃 파일에 같이 갈 것

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

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

당신의에서 onCreate 메소드에서 다음과 같이 사용할 수 있습니다

<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" /> 
관련 문제