2010-11-09 34 views
16

Tôi đặt hình ảnh làm nút Backgroud, nhưng Nút không có điểm nhấn khi nhấp vào nút đó. Có cách nào để giải quyết nó không.Làm cách nào để làm nổi bật Nút?

+1

Tham khảo này [link] (http://stackoverflow.com/questions/4125774/android-button-set-onclick-background-image-change-with-xml/4125877#4125877) – David

Trả lời

10

Để đưa bạn đến đúng tài nguyên ở đây là tài liệu của StateList Drawable. Bạn chỉ cần tạo và xác định nó trong thư mục con res/drawable và đặt nó làm nền nút của bạn.

+0

Câu trả lời tuyệt vời! Bạn đang làm gì ở đây trong quá khứ? ;-) –

13

Xem here

Và cũng here cho Romain Guy của câu trả lời:

Trong res/drawable, tạo ra một tập tin gọi là ví dụ mybutton_background.xml và đặt một cái gì đó giống như bên này:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_focused="true" android:state_pressed="false" 
     android:drawable="@drawable/button_background_focus" /> 

    <item android:state_focused="true" android:state_pressed="true" 
     android:drawable="@drawable/button_background_pressed" /> 

    <item android:state_focused="false" android:state_pressed="true" 
     android:drawable="@drawable/button_background_pressed" /> 

    <item android:drawable="@drawable/button_background_normal" /> 

</selector> 

Sau đó, đặt này có thể vẽ được là t nền của nút của bạn với android:background="@drawable/mybutton_background"

+0

Tôi có thể' Tôi truy cập trang web đó, vì tôi ở Trung Quốc .... cảm ơn. – Nick

+0

Bạn có thể sử dụng máy chủ proxy để đến đó. Dù sao, cập nhật câu trả lời của tôi với các giải pháp (một trong những viết bởi Romain Guy) –

4

nếu bạn đang sử dụng

<Button 
    ------------------ 
android:background="@drawable/youimage" 
> 

Sau đó, vì nó là tốt hơn để tuyên bố mới một xml mới. với hình ảnh bạn có thể chỉ định hình ảnh cho mỗi event state. như Octavian nói
và bạn có thể bạn có thể đặt xml này như bạn nền

nếu xml của bạn là 'res/drawable/abc.xml' sau đó thiết lập nền như

android:background="@drawable/abc" 

Bạn cũng có thể sử dụng ImageButton

<ImageButton 
---------------------- 
android:src="@drawable/youimage" 
/> 

Lợi ích cho ImageButton là, không cần hình ảnh khác để làm nổi bật.

0

Sử dụng View.setFocusableInTouchMode hoặc android: focusableInTouchMode. Trong trường hợp này, hãy sử dụng phần này cho Nút của bạn, trong XML hoặc trong chính mã đó. Để biết chi tiết tham khảo: http: //developer.android.com/reference/android/view/View.html

+0

Tôi là Trung Quốc, có GFW ở đây, vì vậy tôi không thể http://developer.android.com/ cảm ơn anyway – Nick

2

tôi đã thực hiện nó bằng cách ghi đè Nút lớp

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 
public void setPressedBg(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 setPressedBg(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); 
} 

}

trong tôi main()

toneButton.setPressedBg(R.drawable.button_tone_protrait_pipa, 
      R.drawable.button_tone_protrait_pipa1, 
      R.drawable.button_tone_protrait_pipa1); 
3

Hãy thử điều này để có được hiệu ứng làm nổi bật của người nghèo mà không cần tạo nhiều bản sao hình ảnh của bạn. Thiết lập giá trị alpha 0,8 đến một giá trị mong muốn:

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.widget.ImageButton; 

public class EAImageButton extends ImageButton { 

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

    } 
    public EAImageButton(Context context, AttributeSet attrs){ 
     super(context,attrs); 
    } 
    public EAImageButton(Context context, AttributeSet attrs, int defStyle){ 
     super(context,attrs,defStyle); 
    } 
    public void setImageResource (int resId){ 
     super.setImageResource(resId); 
    } 
    @Override 
    public boolean onTouchEvent(MotionEvent e){ 
     if(e.getAction() == MotionEvent.ACTION_DOWN){ 
      this.setAlpha((int)(0.8 * 255)); 
     }else if(e.getAction() == MotionEvent.ACTION_UP){ 
      this.setAlpha((int)(1.0 * 255)); 
     } 

     return super.onTouchEvent(e); 

    } 

} 
+0

+100 Sạch sẽ và thông minh. –

20

Nếu đây là một ImageButton và bạn không muốn sử dụng một vài drawable cho mỗi ép bang/unpressed bạn có thể sử dụng attibute lọc màu sắc của hình ảnh. Giải pháp này tương tự như giải pháp được Omar sử dụng.

Tạo OnTouchListener sửa đổi bộ lọc màu khi chạm.

public class ButtonHighlighterOnTouchListener implements OnTouchListener { 

    final ImageButton imageButton; 

    public ButtonHighlighterOnTouchListener(final ImageButton imageButton) { 
    super(); 
    this.imageButton = imageButton; 
    } 

    public boolean onTouch(final View view, final MotionEvent motionEvent) { 
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
     //grey color filter, you can change the color as you like 
     imageButton.setColorFilter(Color.argb(155, 185, 185, 185)); 
    } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) { 
     imageButton.setColorFilter(Color.argb(0, 185, 185, 185)); 
    } 
    return false; 
    } 

} 

Gán nghe này để nút của bạn:

myButton = (ImageButton) findViewById(R.id.myButton); 
    myButton.setOnTouchListener(new ButtonHighlighterOnTouchListener(myButton)); 

Cập nhật

Cải thiện lớp để áp dụng highlighter để ImageView, ImageButton hoặc TextView qua drawable hợp chất của nó.

public class ButtonHighlighterOnTouchListener implements OnTouchListener { 

    private static final int TRANSPARENT_GREY = Color.argb(0, 185, 185, 185); 
    private static final int FILTERED_GREY = Color.argb(155, 185, 185, 185); 

    ImageView imageView; 
    TextView textView; 

    public ButtonHighlighterOnTouchListener(final ImageView imageView) { 
    super(); 
    this.imageView = imageView; 
    } 

    public ButtonHighlighterOnTouchListener(final TextView textView) { 
    super(); 
    this.textView = textView; 
    } 

    public boolean onTouch(final View view, final MotionEvent motionEvent) { 
    if (imageView != null) { 
     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
     imageView.setColorFilter(FILTERED_GREY); 
     } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) { 
     imageView.setColorFilter(TRANSPARENT_GREY); // or null 
     } 
    } else { 
     for (final Drawable compoundDrawable : textView.getCompoundDrawables()) { 
     if (compoundDrawable != null) { 
      if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
      // we use PorterDuff.Mode. SRC_ATOP as our filter color is already transparent 
      // we should have use PorterDuff.Mode.LIGHTEN with a non transparent color 
      compoundDrawable.setColorFilter(FILTERED_GREY, PorterDuff.Mode.SRC_ATOP); 
      } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) { 
      compoundDrawable.setColorFilter(TRANSPARENT_GREY, PorterDuff.Mode.SRC_ATOP); // or null 
      } 
     } 
     } 
    } 
    return false; 
    } 

} 
+0

Chỉ cần một chỉnh sửa: Bạn phải trả lại true trên onTouch() để báo cho SO biết rằng bạn đã thực hiện một số hành động trong hành động chạm. –

+0

đây là những gì tôi đang tìm kiếm .. imageView.setColorFilter() hoạt động hoàn hảo –

Các vấn đề liên quan