首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

两种button点击后改变色彩的方法selectot和重写

2012-08-22 
两种button点击后改变颜色的方法selectot和重写1.mybutton_background.xml?xml version1.0 encoding

两种button点击后改变颜色的方法selectot和重写

1.

mybutton_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector 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 drawable="@drawable/button_background_normal">
</selector>
android:background="@drawable/mybutton_background"

?

2.

public class OnPressButton extends Button{

public OnPressButton(Context context) {
super(context);
}

public OnPressButton(Context context, AttributeSet attrs){
super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
//sets the button image based on whether the button in its pressed state
setBackgroundDrawable(getResources().getDrawable(isPressed()?R.drawable.btn_on : R.drawable.btn_off));
super.onDraw(canvas);
}

}

?

<view

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My Button" />

?

这句话要注意android:background="@android:color/transparent"

也可以查看http://code.google.com/android/toolbox/custom-components.html

热点排行