나는 이것이 어리석은 질문이지만 Google 검색은 나에게 좋은 대답을주지 않았 음을 알고 있습니다. 내 활동에 버튼이 있지만 그것을 누르면 시각적으로 볼 수 없습니다. 버튼이 잘 작동하지만. xml 파일에서 뭔가를해야한다는 것을 알지만 무엇이 확실하지 않습니다. 수동으로 두 장의 사진을 업로드하지 않아도 이미지 버튼을 자동으로 약간 변경하는 방법이 있습니까?클릭 한 기능이있는 맞춤 이미지 버튼
2
A
답변
0
import android.content.Context;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.widget.Button;
/**
* Applies a pressed state color filter or disabled state alpha for the button's background
* drawable.
*
* @author shiki
*/
public class SAutoBgButton extends Button {
public SAutoBgButton(Context context) {
super(context);
}
public SAutoBgButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SAutoBgButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setBackgroundDrawable(Drawable d) {
// Replace the original background drawable (e.g. image) with a LayerDrawable that
// contains the original drawable.
SAutoBgButtonBackgroundDrawable layer = new SAutoBgButtonBackgroundDrawable(d);
super.setBackgroundDrawable(layer);
}
/**
* The stateful LayerDrawable used by this button.
*/
protected class SAutoBgButtonBackgroundDrawable extends LayerDrawable {
// The color filter to apply when the button is pressed
protected ColorFilter _pressedFilter = new LightingColorFilter(Color.LTGRAY, 1);
// Alpha value when the button is disabled
protected int _disabledAlpha = 100;
public SAutoBgButtonBackgroundDrawable(Drawable d) {
super(new Drawable[] { d });
}
@Override
protected boolean onStateChange(int[] states) {
boolean enabled = false;
boolean pressed = false;
for (int state : states) {
if (state == android.R.attr.state_enabled)
enabled = true;
else if (state == android.R.attr.state_pressed)
pressed = true;
}
mutate();
if (enabled && pressed) {
setColorFilter(_pressedFilter);
} else if (!enabled) {
setColorFilter(null);
setAlpha(_disabledAlpha);
} else {
setColorFilter(null);
}
invalidateSelf();
return super.onStateChange(states);
}
@Override
public boolean isStateful() {
return true;
}
}
}
이를 사용하려면이 같은 원래의 버튼 선언을 대체 :
<Button
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/button_blue_bg"
android:text="Button with background image" />
을이 수행
: 여기<com.buttondemo.SAutoBgButton
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/button_blue_bg"
android:text="Button with background image" />
이 사용자 정의 버튼을 사용하여 샘플 출력입니다
희망 사항 :
1
당신은 사용하여 유하려면 버튼을 비활성화 할 수 있습니다
button.setEnabled(false);
& 또한 사용하여 다시 활성화 할 수 있습니다
button.setEnabled(true);
편집 :
당신은 또한을 변경하여 시도 할 수 있습니다 단추의 배경색은 다음과 같습니다.
onClick()
{
button.setBackgroundColor(#CCCCCC);
}
이 링크를 확인해주세요. 나는 이것이 당신이 원하는 것이라고 생각합니다. http://stackoverflow.com/a/14810912 –