如何使button突出显示?

我设置了一个图像作为button背景,但button没有高亮点击它。 有什么办法可以解决吗

Solutions Collecting From Web of "如何使button突出显示?"

为了让你得到正确的资源,这里是StateList Drawable的文档。 您只需在res/drawable子目录中创build并定义它,并将其设置为您的button背景。

如果这是一个ImageButton,并且您不想为每个按下/未按下状态使用多个drawable,则可以使用图像的颜色filter属性。 此解决scheme与Omar使用的解决scheme类似。

创build一个OnTouchListener触摸修改颜色filter。

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

将此侦听器分配给您的button:

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

更新

改进的类,通过其复合Drawable应用荧光笔到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; } } 

看到这里

罗曼盖伊的答案也在这里 :

在res / drawable中,创build一个名为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> 

然后用android:background="@drawable/mybutton_background"设置这个drawable作为你的button的背景。

如果你正在使用

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

那么最好声明一个新的xml。 与图像,您可以指定每个事件状态的图像。 像Octavian说的那样
你可以设置这个XML作为你的背景

如果你的XML是'res / drawable / abc.xml',那么设置背景为

 android:background="@drawable/abc" 

你也可以使用ImageButton

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

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

尝试这种方式来获得穷人的突出效果,而不需要制作多个图像副本。 将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); } } 

我通过覆盖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); 

使用View.setFocusableInTouchModeandroid: focusableInTouchMode 。 在这种情况下,使用这个片断为您的Button,无论是在XML或代码本身。 有关详细信息, 请参阅:http://developer.android.com/reference/android/view/View.html