簡體   English   中英

如何使您的自定義視圖可聚焦?

[英]How to make your custom view focusable?

我正在創建一個自定義視圖。 這基本上是一個帶有邊框的矩形框。 我希望邊框改變顏色並在用戶點擊它時使其成為焦點。 如果矩形失去焦點,我希望邊框恢復到原始顏色。 我想將此背景用作表單背景。 我已經嘗試過 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) {

    }


}

在觸摸模式下,您的視圖不會自動聚焦。 如果您希望您的視圖在被點擊(觸摸)時獲得焦點,您必須調用“setFocusableInTouchMode()”。雖然過時了,但我發現這個解釋對於描述觸摸模式很有用。

因此,在您的init()添加setFocusableInTouchMode(true)

其次,無論視圖是否聚焦,您都不會在onDraw()做任何不同的事情。 將其更改為如下所示:

protected void onDraw(Canvas canvas) {
    // your paint code
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (isFocused()) { // draw the border
            canvas.drawRoundRect(0, 0, getWidth(), getHeight(), 10, 10, strokePaint);
            canvas.drawRoundRect(0 + 3, 0 + 3, getWidth() - 3, getHeight() - 3, 7, 7, fillPaint);
        } else { // don't draw the border
            canvas.drawRoundRect(0, 0, getWidth(), getHeight(), 7, 7, fillPaint);
        }
    } else {
        //similar here
    }
    // The rest of your code
}

嘗試這個:

public class FormBackGroundView extends View {

    private int _borderColor;
    Paint fillPaint, strokePaint;
    Canvas canvas;
    private boolean isFocused;

    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();
    }

    private void init() {
        fillPaint = new Paint();
        strokePaint = new Paint();
        fillPaint.setStyle(Paint.Style.FILL);
        strokePaint.setStyle(Paint.Style.FILL);
        strokePaint.setAntiAlias(true);
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN && !isFocused) {
                    isFocused = true;
                    invalidate();
                } else if (motionEvent.getAction() == MotionEvent.ACTION_UP && isFocused) {
                    isFocused = false;
                    invalidate();
                }
                return true;
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        fillPaint.setColor(Color.WHITE);

        strokePaint.setColor(isFocused? Color.RED:Color.BLACK);


        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);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM