2011-08-29 26 views
32

Tôi có một ImageButton bị tắt (không thể nhấp hoặc được đặt là bị tắt). Tôi muốn cung cấp cho một giao diện người dùng cảm thấy cho người dùng rằng nó bị vô hiệu hóa mà không sử dụng bất kỳ hình ảnh nào khác.Android ImageButton với giao diện người dùng bị vô hiệu hóa cảm thấy

Có cách nào để thực hiện việc này không?

+0

Bạn có nghĩa là bạn muốn khung làm việc của Android chuyển sang màu xám hoặc một cái gì đó tương tự như vậy nếu bạn vô hiệu hóa nó? – C0deAttack

+0

có. Tôi muốn chính xác giống như –

+0

C0deTạm ý bất kỳ ý tưởng nào ..? –

Trả lời

49

Không giống như thường lệ Button, một ImageButton hoặc Button có nền hình ảnh không bị chuyển sang màu xám khi bị tắt. Bạn thực sự phải sử dụng một hình ảnh khác hoặc xử lý nó theo cách nó có màu xám.

nên sử dụng hình ảnh khác là ok, bạn có thể làm điều này bằng cách sử dụng một <selector> (ở đây liên quan đến một thường xuyên Button nhưng điều này amongs với cùng):

  • /drawable/my_selector.xml:

    <?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
        <item android:state_enabled="false" 
         android:drawable="@drawable/button_gray" /> ***button_gray is a Drawable image*** 
        <item android:state_pressed="true" 
         android:drawable="@drawable/button_gray" /> 
        <item android:drawable="@drawable/button_red" /> ***button_red is a Drawable image*** 
    </selector> 
    

Xin lưu ý rằng trong bộ chọn, logic áp dụng một cách tuần tự, mục cho mỗi mục. Ở đây, button_red được sử dụng mọi lúc nhưng khi nút bị tắt hoặc bị đẩy.

  • bạn layout.xml:

    <Button android:id="@+id/myButton" 
         android:background="@drawable/my_selector" ***this is a reference to the selector above *** 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
    /> 
    

Và nên sử dụng hình ảnh khác là một vấn đề, câu trả lời khác (chẳng hạn như @ Tronman là hoặc @ Southerton của) cung cấp cho bạn cách để lập trình xử lý hình ảnh trong một cách nó xuất hiện màu xám.

+1

+1 cho câu trả lời :) điều này thực sự hữu ích :) – Deepthi

+0

Có một cách khác. Xem giải pháp của tôi dưới đây. – tronman

+0

Tôi thấy rằng thứ tự của các yếu tố quan trọng trong my_selector. Mục của state_enabled = "false" phải được đặt đầu tiên của giá trị mặc định. –

-2

Nope, hình ảnh của bạn là biệt yếu tố .. vì vậy nếu bạn không muốn thay đổi hình ảnh sau đó bạn không thể nói cho dù nút hình ảnh bị vô hiệu hóa, kích hoạt, nhấn.

3

Bạn có thể đặt thành không thể nhấp và cũng có thể đặt alpha để hiển thị cảm giác mà bạn đề cập.

28

@Oleg Vaskevich đã đưa ra một giải pháp khác nhau cho vấn đề ở đây: Disable an ImageButton

Giải pháp của ông cho phép bạn màu xám-out một ImageButton mà không cần tạo hình ảnh bổ sung hoặc sử dụng một <selector>.

/** 
* Sets the image button to the given state and grays-out the icon. 
* 
* @param enabled The state of the button 
* @param item The button item to modify 
* @param iconResId The button's icon ID 
*/ 
public static void setImageButtonEnabled(Context ctxt, boolean enabled, 
     ImageButton item, int iconResId) { 

    item.setEnabled(enabled); 
    Drawable originalIcon = ctxt.getResources().getDrawable(iconResId); 
    Drawable icon = enabled ? originalIcon : convertDrawableToGrayScale(originalIcon); 
    item.setImageDrawable(icon); 
} 

/** 
* Mutates and applies a filter that converts the given drawable to a Gray 
* image. This method may be used to simulate the color of disable icons in 
* Honeycomb's ActionBar. 
* 
* @return a mutated version of the given drawable with a color filter applied. 
*/ 
public static Drawable convertDrawableToGrayScale(Drawable drawable) { 
    if (drawable == null) 
     return null; 

    Drawable res = drawable.mutate(); 
    res.setColorFilter(Color.GRAY, Mode.SRC_IN); 
    return res; 
} 
+3

Đây là câu trả lời hay nhất vì nó giải quyết yêu cầu của op không sử dụng hình ảnh khác. Cảm ơn các giải pháp. –

+0

Cảm ơn bạn rất nhiều! Câu trả lời tuyệt vời. – DmitryKanunnikoff

+0

Vâng, giải pháp tuyệt vời! – FractalBob

4

Xây dựng về câu trả lời @tronman bạn cũng có thể soạn một chức năng mà sẽ xám ra drawables nạp tự động (ví dụ: không phải từ nguồn lực, - ví dụ nạp từ file svg liệu và chuyển đổi sang BitmapDrawables on the fly).

/** 
* Sets the specified image buttonto the given state, while modifying or 
* "graying-out" the icon as well 
* 
* @param enabled The state of the menu item 
* @param item The menu item to modify 
* @param originalIcon The drawable 
*/ 
public static void setImageButtonEnabled(Context ctxt, boolean enabled, ImageButton item, Drawable originalIcon) { 
    item.setEnabled(enabled); 

    Drawable res = originalIcon.mutate(); 
    if (enabled) 
     res.setColorFilter(null); 
    else 
     res.setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN); 
} 

Nếu bạn cũng có thể vẽ không minh bạch trên nền (thiết lập với android: background) tham khảo selectors Android: How to Make A Drawable Selector cũng để thay đổi nền.

1

Tôi ưa thích ghi đè phương thức setEnabled() trong ImageButton để thay đổi thuộc tính alpha của hình ảnh cho phù hợp. Vì vậy, khi nút bị vô hiệu hóa, hình ảnh sẽ được minh bạch một phần và trông dễ bị tắt hơn.

public class CustomImageButton extends ImageButton { 
    //... 

    @Override 
    public void setEnabled(boolean enabled) { 
     if(this.isEnabled() != enabled) { 
      this.setImageAlpha(enabled ? 0xFF : 0x3F); 
     } 
     super.setEnabled(enabled); 
    } 
} 
0
public static void setImageButtonEnabled(@NonNull final ImageView imageView, 
             final boolean enabled) { 
    imageView.setEnabled(enabled); 
    imageView.setAlpha(enabled ? 1.0f : 0.3f); 

    final Drawable originalIcon = imageView.getDrawable(); 
    final Drawable icon = enabled ? originalIcon : convertDrawableToGrayScale(originalIcon); 
    imageView.setImageDrawable(icon); 
} 

private static Drawable convertDrawableToGrayScale(@NonNull Drawable drawable) { 
    final ColorMatrix matrix = new ColorMatrix(); 
    matrix.setSaturation(0); 
    final ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix); 

    final Drawable mutated = drawable.mutate(); 
    mutated.setColorFilter(filter); 

    return mutated; 
} 

Nếu bạn sử dụng Kotlin, bạn có thể tạo ra một chức năng mở rộng thay vì, vì vậy nó trông thanh lịch hơn:

fun ImageView.setImageButtonEnabled(enabled: Boolean){ 
    //Above implementation here 
} 

và gọi nó là sử dụng:

yourImageView.setImageButtonEnabled(true/false) 
Các vấn đề liên quan