【发布时间】:2017-10-05 23:12:01
【问题描述】:
我正在创建一个自定义视图。这基本上是一个带有边框的矩形框。我希望边框在用户单击时更改颜色并使其具有焦点。如果矩形失去焦点,我希望边框恢复原始颜色。我想用这个背景作为表单背景。我已经尝试过 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) {
}
}
【问题讨论】:
标签: java android android-layout android-custom-view