2017-10-05 4 views
2

사용자 지정보기를 만들고 있습니다. 이것은 기본적으로 테두리가있는 사각형 상자입니다. 나는 테두리를 색을 바꾸고 사용자가 클릭 할 때 초점을 맞추기를 원합니다. 직사각형에 포커스가 없으면 테두리를 원래 색으로 되돌리고 싶습니다. 이 배경을 양식 배경으로 사용하고 싶습니다. 안드로이드 문서 및 스택 오버플로 해답을 시도하고이 작업을 수행 할 수 없습니다. 클릭 할 수있게 만들었지 만 그 이상으로 진행할 수는 없습니다.사용자 정의보기를 포커스 할 수있게 만드는 방법은 무엇입니까?

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 - 대응 - to-touch) – 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()가 Google에서 호출 한 경우? – 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