自定义Button
程序员文章站
2022-03-17 10:38:50
...
1.CustomButton.java文件
2.ButtonTheme.java文件
以上代码即可实现点击按钮的时候切换不同的按钮背景
package com.example;
import android.app.Activity;
import android.os.Bundle;
public class CustomButton extends Activity {
// Called when the activity is first created.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
2.ButtonTheme.java文件
package com.example;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.Button;
public class ButtonTheme extends Button {
public ButtonTheme(Context context) {
super(context);
}
public ButtonTheme(Context context, AttributeSet attrs) {
super(context, attrs);
}
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_down : R.drawable.btn_on));
super.onDraw(canvas);
}
}
以上代码即可实现点击按钮的时候切换不同的按钮背景
上一篇: 06 方法定义及重载
推荐阅读