如何使按钮突出显示?

时间:2010-11-09 08:22:14

标签: android

我将图像设置为按钮背景,但按钮在单击时没有突出显示。 有没有办法解决它。

7 个答案:

答案 0 :(得分:21)

如果这是一个ImageButton,并且您不想为每个按下/未按下状态使用多个drawable,则可以使用图像的颜色过滤器。该解决方案类似于Omar使用的解决方案。

创建一个OnTouchListener,在触摸时修改颜色过滤器。

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;
  }

}

将此侦听器分配给您的按钮:

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

<强>更新

改进了类,通过其复合Drawable将Hig​​hlighter应用于ImageView,ImageButton或TextView。

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;
  }

}

答案 1 :(得分:13)

请参阅here

还有here 对于Romain Guy的回答:

  

在res / drawable中,创建一个名为mybutton_background.xml的文件       并把这样的东西放在里面:

<?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> 

然后将此drawable设置为按钮的背景     android:background="@drawable/mybutton_background"

答案 2 :(得分:11)

为了帮助您找到正确的资源,请访问StateList Drawable的文档。您只需在res/drawable子目录中创建并定义它,并将其设置为按钮背景。

答案 3 :(得分:4)

如果您正在使用

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

然后,因为最好声明一个新的xml。使用图像,您可以为每个event state指定图像。正如屋大维所说 你可以将这个xml设置为背景

如果您的xml是'res / drawable / abc.xml',则将背景设置为

android:background="@drawable/abc"

您也可以使用ImageButton

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

ImageButton的好处是,不需要不同的图像来突出显示。

答案 4 :(得分:3)

尝试这样做可以获得一个穷人的高光效果,而无需制作多个图像副本。将alpha值0.8设置为所需的值:

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);

    }

}

答案 5 :(得分:2)

我是通过覆盖Button类

来实现的
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);
}

}

在我的主要()

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

答案 6 :(得分:0)

使用 View.setFocusableInTouchMode android:focusableInTouchMode 。在这种情况下,将此部分用于Button,可以是XML或代码本身。有关详细信息,请参阅:http://developer.android.com/reference/android/view/View.html