我正在创建一个自定义视图。这基本上是一个带边框的矩形框。我希望边框可以改变颜色,并在用户点击时使其可以聚焦。如果矩形失去焦点,我想让边框恢复原来的颜色。我想用这个背景作为表单背景。我已经尝试过android文档和堆栈溢出的答案,我无法做到这一点。我让它变得可点击,但我无法继续进行下去。如何使您的自定义视图可以调焦?
public class FormBackgroundView extends View implements View.OnClickListener{
private int _borderColor;
Paint fillPaint, strokePaint;
Canvas canvas;
public FormBackgroundView(Context context) {
super(context);
init();
}
public FormBackgroundView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public FormBackgroundView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public FormBackgroundView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init(){
super.setOnClickListener(this);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
fillPaint = new Paint();
strokePaint = new Paint();
fillPaint.setStyle(Paint.Style.FILL);
fillPaint.setColor(Color.WHITE);
strokePaint.setStyle(Paint.Style.FILL);
strokePaint.setColor(Color.BLACK);
strokePaint.setAntiAlias(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
canvas.drawRoundRect(0,0,getWidth(),getHeight(), 10, 10, strokePaint);
canvas.drawRoundRect(0 + 3,0 + 3,getWidth() - 3,getHeight() - 3, 7, 7, fillPaint);
} else {
canvas.drawRect(0,0,getWidth(),getHeight(), strokePaint);
canvas.drawRect(0 + 4,0 + 4,getWidth() -4 ,getHeight() -4, fillPaint);
}
this.canvas = canvas;
}
@Override
public void onClick(View view) {
}
}
的[自定义视图不响应触摸可能的复制](https://stackoverflow.com/questions/10406354/custom-view-not - 对应于触摸) – Danieboy