`

自定义Button

阅读更多
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);
	}
}


以上代码即可实现点击按钮的时候切换不同的按钮背景
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics