使用按钮小部件处理自定义视图中的事件(在画布上绘图)

时间:2016-02-11 18:04:28

标签: java android

首先抱歉我的英语。我有一个CustomView,我使用Canvas绘制形状。当我点击屏幕上的按钮时,我需要绘制正方形。为什么当我点击按钮时没有任何反应?

MainActivity

public class MainActivity extends Activity {

        CustomView mView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mView = new CustomView(this);
        }

        public void mButton1(View view) {
            mView.drawSquare(1);
        }

        public void mButton2(View view) {
            mView.drawSquare(2);
        }

        public void mButton3(View view) {
            mView.drawSquare(3);
        }
    }

CustomView

public class CustomView extends View {

    private Paint mPaint = new Paint();
    private int drawingSquare = 0;

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

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawARGB(80, 102, 204, 255);
        if (drawingSquare == 1) {
            canvas.drawRect(getWidth()/2-125, getHeight()/2-25, getWidth()/2-75, getHeight()/2+25, mPaint);
        } else if (drawingSquare == 2) {
            canvas.drawRect(getWidth()/2-25, getHeight()/2-25, getWidth()/2+25, getHeight()/2+25, mPaint);
        } else if (drawingSquare == 3) {
            canvas.drawRect(getWidth()/2+75, getHeight()/2-25, getWidth()/2+125, getHeight()/2+25, mPaint);
        }
    }

    public void drawSquare(int mSquareNumber) {
        this.drawingSquare = mSquareNumber;
        invalidate();
    }
}

activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">

        <com.stackoverflow.myapplication.CustomView
            android:id="@+id/myCustomView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="10">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1"
            android:id="@+id/button1"
            android:layout_weight="1"
            android:onClick="mButton1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2"
            android:id="@+id/button2"
            android:layout_weight="1"
            android:onClick="mButton2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button3"
            android:id="@+id/button3"
            android:layout_weight="1"
            android:onClick="mButton3" />
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

问题是您没有使活动中使用的视图无效。

你需要做findViewById,它会工作。所有其他代码都写得很好。

public class MainActivity extends Activity {

        CustomView mView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mView = findViewById(R.id.myCustomView);
        }

        public void mButton1(View view) {
            mView.drawSquare(1);
        }

        public void mButton2(View view) {
            mView.drawSquare(2);
        }

        public void mButton3(View view) {
            mView.drawSquare(3);
        }
    }

答案 1 :(得分:-1)

您需要在自定义视图中处理触摸事件(使用onTouchEvent),检查触摸事件的坐标,并以您希望的方式响应这些事件。这些都不会自动发生在自定义视图中。您将不得不学习编写代码。