私はカスタムビューを作成しています。これは基本的に矩形の枠で囲まれたボックスです。ボーダーが色を変更して、ユーザーがクリックすると、そのボーダーをフォーカス可能にします。矩形がフォーカスを失った場合、境界線を元の色に戻したい。私はフォームの背景としてこの背景を使いたいです。私はアンドロイドのドキュメントとスタックオーバーフローの答えを試して、私はこれを行うことはできません。私はそれをクリック可能にしたが、それ以上は進めることができない。カスタムビューをフォーカス可能にするにはどうすればいいですか?
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対応するto-touch) – Danieboy