以下为MyButton类:
package com.TouchLife.touchlife;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
public class MyButton extends android.widget.Button{
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
private boolean focused;
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
this.focused = focused;
if(focused){
android.view.ViewGroup.LayoutParams temp = this.getLayoutParams();
temp.height = this.getHeight() - 1;
temp.width = this.getWidth() - 1;
this.setLayoutParams(temp);
}
else{
android.view.ViewGroup.LayoutParams temp = this.getLayoutParams();
temp.height = this.getHeight() + 1;
temp.width = this.getWidth() + 1;
this.setLayoutParams(temp);
}
}
@Override
protected void onDraw(android.graphics.Canvas canvas) {
//this.getba
if(focused){
Paint paint = new Paint();
paint.setColor(0xeeFAC23D);
canvas.drawLine(1, 1, this.getWidth()-1, 1, paint);
canvas.drawLine(1, 1, 1, this.getHeight()-1, paint);
canvas.drawLine(1, this.getHeight()-1, this.getWidth()-1, this.getHeight()-1, paint);
canvas.drawLine(this.getWidth()-1, 1, this.getWidth()-1, this.getHeight()-1, paint);
}
super.onDraw(canvas);
}
}
使用方法:
xml布局文件直接引用:
本文介绍了如何重构Button类,以便在大屏幕应用中,当使用遥控器操作时,按钮在获取焦点时能显示清晰的边框,提升用户体验。

被折叠的 条评论
为什么被折叠?



