2017-10-05 28 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()が呼び出されたときに1つのことを聞きたい。私はそれが1回だけ呼ばれたと思った。フォーカスが変わったときに再び呼び出されますか? – 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

このコードには1つの問題があります。フォームが接触しなくなると、フォームは通常の状態に戻ります。 – killerprince182

関連する問題