Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用按钮小部件处理自定义视图(在画布上绘制)中的事件_Java_Android - Fatal编程技术网

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

Java 使用按钮小部件处理自定义视图(在画布上绘制)中的事件,java,android,Java,Android,首先为我的英语感到抱歉。我有一个自定义视图,在这里我使用画布绘制形状。我需要当我点击屏幕上的按钮时被画成正方形。为什么当我点击按钮时什么都没发生 main活动 public class MainActivity extends Activity { CustomView mView; @Override protected void onCreate(Bundle savedInstanceState) { super.

首先为我的英语感到抱歉。我有一个自定义视图,在这里我使用画布绘制形状。我需要当我点击屏幕上的按钮时被画成正方形。为什么当我点击按钮时什么都没发生

main活动

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);
        }
    }
自定义视图

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();
    }
}
主要活动

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


在自定义视图中,您需要一些东西来处理触摸事件(使用),检查触摸事件的坐标,并以您想要的方式响应这些事件。对于自定义视图,这些都不会自动发生。您必须学会编写代码。

问题在于您没有使活动中使用的视图无效

你需要做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);
        }
    }