2017-10-05 81 views
2

我正在创建一个自定义视图。这基本上是一个带边框的矩形框。我希望边框可以改变颜色,并在用户点击时使其可以聚焦。如果矩形失去焦点,我想让边框恢复原来的颜色。我想用这个背景作为表单背景。我已经尝试过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) { 

    } 


} 
+1

的[自定义视图不响应触摸可能的复制](https://stackoverflow.com/questions/10406354/custom-view-not - 对应于触摸) – Danieboy

回答

1

你的观点是不是在触摸模式下自动可获得焦点。你必须调用“setFocusableInTouchMode()`如果你希望你的视图获取焦点被点击时(感动)。虽然过时了,我发现this explanation适用于描述触摸模式。

所以,在你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 
} 
+0

感谢您的回答。当onDraw()被调用时,我想问一个问题。我以为它只被称为一次。焦点变化时是否再次调用? – killerprince182

+0

或当我们调用invalidate()时? – killerprince182

+0

@ killerprince182是的,它被称为。对于一个可聚焦的视图,通常会有一些视觉指示表明该视图具有焦点,所以有必要调用“onDraw()”。 – Cheticamp

1

试试这个:

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

此代码有一个问题。只要表格停止触摸,表格就会恢复正常。 – killerprince182

相关问题