2017-10-05 65 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

相關問題