自定义视图可以点击

问题描述:

我想让我的自定义视图被点击。而现在我不知道哪个是错的,当我点击它 时,它什么都没发生。提前感谢。这个视图是我用canvas来画一个戒指,我希望戒指的内部可以点击。自定义视图可以点击

public class CircleProgressBar extends View{ 
    OnClickListener progressButton; 
    private int hour; 
    private int maxProgress = 24; 
    private int progress; 
    int progress1; 
    private int progressStrokeWidth = 32; 

    RectF oval; 
    Paint paint; 
    Paint paint1; 




    public CircleProgressBar(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     oval = new RectF(); 
     paint = new Paint(); 
     paint1 = new Paint(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas){ 
     super.onDraw(canvas); 
     Calendar c=Calendar.getInstance(); 
     hour = c.get(Calendar.HOUR_OF_DAY); 
     progress = hour; 
     int width = this.getWidth(); 
     int height = this.getHeight(); 

     if(width != height){ 

      int min = Math.min(width, height); 
      width = min; 
      height = min; 
     } 
     paint.setAntiAlias(true); 
     paint.setFilterBitmap(true); 
     paint.setColor(Color.LTGRAY); 
     canvas.drawColor(Color.TRANSPARENT); 
     paint.setStrokeWidth(15); 
     paint.setStyle(Style.STROKE); 

     oval.left = progressStrokeWidth/2; // 左上角x 
     oval.top = progressStrokeWidth/2; // 左上角y 
     oval.right =height - progressStrokeWidth/2; // 左下角x 
     oval.bottom = height - progressStrokeWidth/2; // 右下角y 
     canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG)); 
     canvas.drawArc(oval, -90, 360, false, paint); 

     paint.setColor(Color.rgb(0x57, 0x87, 0xb6)); 
     paint.setStrokeCap(Paint.Cap.ROUND); 
     if(progress == 9){ 
      canvas.drawArc(oval, -90, 134, false, paint); 
     }else{ 
     canvas.drawArc(oval, -90, (long)(((float) progress/maxProgress) * 360), false, paint); 
     } 
     paint.setColor(Color.CYAN); 
     paint.setAntiAlias(true); 
     canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG)); 

     canvas.drawArc(oval, -90, 135+0.5f, false, paint); 

     paint.setStrokeWidth(15); 



     String text = (long)(((float) progress/maxProgress) * 100) + "%"; 
     int textHeight = height/4; 
     paint.setTextSize(textHeight); 
     int textWidth = (int) paint.measureText(text, 0, text.length()); 
     paint.setStyle(Style.FILL); 
     canvas.drawText(text, width/2 - textWidth/2, height/2 +textHeight/2, paint); 
    } 
    public int getMaxProgress() { 
     return maxProgress; 
    } 

    public void setMaxProgress(int maxProgress) { 
     this.maxProgress = maxProgress; 
    } 

    public void setProgress(int progress) { 
     this.progress = progress; 
     this.invalidate(); 
    } 



    public void setProgressNotInUiThread(int progress) { 
     this.progress = progress; 
     this.postInvalidate(); 
    } 
    @Override 
    public boolean onTouchEvent(MotionEvent event){ 
     if(event.getAction() == MotionEvent.ACTION_DOWN){ 

     } 
     return true; 

    } 

这是我的主要活动。

public class MainActivity extends Activity{ 

Context context; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.circle_fit); 


    CircleProgressBar progressBar = (CircleProgressBar) findViewById(R.id.circleProgressbar); 
    progressBar.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT).show(); 

     } 
    }); 

,这里是我的xml

<com.example.circlefit.CircleProgressBar 
     android:id="@+id/circleProgressbar" 
     android:layout_width="200dp" 
     android:layout_height="200dp" /> 
+0

只是设置点击为真在你的XML – Praveen

+0

Similer问题是http://*.com/questions/10406354/custom-view-not-responding触摸 –

+0

你想要点击应该在环中工作吗? –

删除自定义类中的触摸监听器。据ovveriding点击功能: -

/

/Remove this piece of code from your class and it will work just fine 
    @Override 
    public boolean onTouchEvent(MotionEvent event){ 
    if(event.getAction() == MotionEvent.ACTION_DOWN){ 

    } 
    return true; 

} 
+0

如何显示按钮效果? – Ethan

+0

您有关于点击侦听器无法正常工作的查询。它现在会工作。你所说的按钮有什么作用? –

+0

点击侦听器正在工作,但它不像我点击按钮。我的意思是如何点击它像按钮effect.Thanks – Ethan

在XML补充一点:

android:clickable="true" 
android:focusable="true" 
+0

它什么都没发生。一路感谢。 – Ethan

尝试这样的:

私人OnClickListener clickListener; private boolean isMoved;

@Override 
public void setOnClickListener(OnClickListener l) { 
    this.clickListener = l; 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     isMoved = false; 
     if (isValidtouchAndIsInsideCircle(event)) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
    if (event.getAction() == MotionEvent.ACTION_MOVE) { 
     isMoved = true; 
    } 
    if (event.getAction() == MotionEvent.ACTION_UP) { 
     if (clickListener != null && !isMoved) { 
      clickListener.onClick(this); 
     } 
     isMoved = false; 
    } 
    return false; 
} 

private boolean isValidtouchAndIsInsideCircle(MotionEvent event) { 
    int xPoint = (int) event.getX(); 
    int yPoint = (int) event.getY(); 
    // here validate the x and y points with your circle coordinates if it 
    // is in circle return true or else return false 
    return false; 
}