可以单击自定义视图

时间:2014-12-26 06:34:04

标签: android view onclicklistener

我想点击我的自定义视图。现在我不知道哪个是错的,当我点击它 它没有发生任何事情。提前谢谢。这个观点是我用画布绘制一个戒指,我希望可以点击戒指的内部。

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" />

3 个答案:

答案 0 :(得分:1)

尝试这样:

私有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;
}

答案 1 :(得分:1)

删除自定义类中的触控侦听器。它覆盖了点击功能: -

/

/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;

}

答案 2 :(得分:0)

在XML中添加:

android:clickable="true"
android:focusable="true"