自定义视图上的android:onClick方法

时间:2017-12-06 09:58:30

标签: java android xml android-layout

我有一个来自GitHub的示例代码。示例效果很好,但是当我只使用部分代码时,我无法正常工作。这是使用自定义视图的XML文件中android:onClick - 方法的问题。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.tapio.testingapp.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <com.example.tapio.testingapp.LVCircularCD
        android:id="@+id/lv_circularCD"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:onClick="startAnim"/>

</RelativeLayout>

Java代码:

public class LVCircularCD extends View {

    private Paint mPaint;

    private float mWidth = 0f;
    private float mPadding = 0f;

    RotateAnimation mProgerssRotateAnim;
    RectF rectF = new RectF();
    RectF rectF2 = new RectF();
    public LVCircularCD(Context context) {
        this(context, null);
    }

    public LVCircularCD(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LVCircularCD(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (getMeasuredWidth() > getHeight())
            mWidth = getMeasuredHeight();
        else
            mWidth = getMeasuredWidth();
        mPadding = 5;


    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
        mPaint.setStrokeWidth(2);
        canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2 - mPadding, mPaint);
        mPaint.setStrokeWidth(3);
        canvas.drawCircle(mWidth / 2, mWidth / 2, mPadding, mPaint);

        mPaint.setStrokeWidth(2);
        rectF = new RectF(mWidth / 2 - mWidth / 3, mWidth / 2 - mWidth / 3,
                mWidth / 2 + mWidth / 3, mWidth / 2 + mWidth / 3);
        canvas.drawArc(rectF, 0, 80
                , false, mPaint);

        canvas.drawArc(rectF, 180, 80
                , false, mPaint);


        rectF2 = new RectF(mWidth / 2 - mWidth / 4, mWidth / 2 - mWidth / 4,
                mWidth / 2 + mWidth / 4, mWidth / 2 + mWidth / 4);
        canvas.drawArc(rectF2, 0, 80
                , false, mPaint);
        canvas.drawArc(rectF2, 180, 80
                , false, mPaint);
        canvas.restore();


    }


    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.WHITE);
        mProgerssRotateAnim = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
                0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mProgerssRotateAnim.setRepeatCount(-1);
        mProgerssRotateAnim.setInterpolator(new LinearInterpolator());//不停顿
        mProgerssRotateAnim.setFillAfter(true);//停在最后


    }

    public void setViewColor(int color)
    {
        mPaint.setColor(color);
        postInvalidate();
    }



    public void startAnim(View view) {
        stopAnim();
        mProgerssRotateAnim.setDuration(1500);
        startAnimation(mProgerssRotateAnim);
    }

    public void startAnim(int time) {
        stopAnim();
        mProgerssRotateAnim.setDuration(time);
        startAnimation(mProgerssRotateAnim);
    }

    public void stopAnim() {
        clearAnimation();
    }


}

有问题的地方是XML文件:android:onclick = startAnim。我将收到以下错误:"Corresponding method handler 'public void startAnim(android.view.View)' not found"

1 个答案:

答案 0 :(得分:0)

是的。我将startAnim方法添加到MainActivity-activity中。工作良好。我仍然想知道原作为何如此有效。