欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义Button

程序员文章站 2022-03-17 10:38:50
...
1.CustomButton.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);
}
}


以上代码即可实现点击按钮的时候切换不同的按钮背景
相关标签: Android Java OS