这是我的main_activity.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="barnquilt.nkc.com.barnquiltmaker.MainActivity">

    <view
        android:layout_width="500dp"
        android:layout_height="500dp"
        class="barnquilt.nkc.com.barnquiltmaker.Qulits.MainDrawingView"
        android:id="@+id/single_touch_view"
        android:background="#ffffff"
        />




我想在视图标签中添加一个按钮,以便onClick转到我的MainDrawingView类而不是MainActivity类。

我已经试过了:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="barnquilt.nkc.com.barnquiltmaker.MainActivity">

    <view
        android:layout_width="500dp"
        android:layout_height="500dp"
        class="barnquilt.nkc.com.barnquiltmaker.Qulits.MainDrawingView"
        android:id="@+id/single_touch_view"
        android:background="#ffffff"

        <Button
         <!--
          Add layout width and layout height here
          -->
         android:text = "test"
         android:onClick = "changeColor"
        />
        />




</RelativeLayout>


它说方法ChangeColor没有在MainActivity中定义。我将如何做到这一点,然后转到我的MainDraingView类?谢谢!
这是MainDrawingView类:

package barnquilt.nkc.com.barnquiltmaker.Qulits;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import barnquilt.nkc.com.barnquiltmaker.BitMap.blue;
import barnquilt.nkc.com.barnquiltmaker.BitMap.red;
import barnquilt.nkc.com.barnquiltmaker.R;

/**
 * Created by pokem on 6/1/2017.
 */

//The 2 * 2 barn quilt
public class MainDrawingView extends View {

    public float eventX;
    public float eventY;

   public List<Point>redPointList = new ArrayList<Point>();
    public List<Point>bluePointList = new ArrayList<Point>();

    public Point redPoint = new Point();
    public Point bluePoint  = new Point();



    Context context = getContext();
   //Bitmaps
   public Bitmap redSquare;
   public Bitmap blueSquare;

   //Colors
   public boolean red = false;
   public boolean blue = false;
   //Squares

    public int width = context.getResources().getDisplayMetrics().widthPixels;
    public int height = context.getResources().getDisplayMetrics().heightPixels;
    public int center  = height/2;
    public int widthDiv = width/2;


    private Paint redPaint  = new Paint();
    private Paint bluePaint = new Paint();
    private Paint black = new Paint();
    private Point pointStart  = new Point();
    private Point pointEnd =  new Point();

    public MainDrawingView(Context context, AttributeSet attrs){
        super(context, attrs);
        redPaint.setAntiAlias(true);
        redPaint.setStrokeWidth(5f);
        redPaint.setColor(Color.RED);
        redPaint.setStyle(Paint.Style.STROKE);
        redPaint.setStrokeJoin(Paint.Join.ROUND);


        bluePaint.setAntiAlias(true);
        bluePaint.setStrokeWidth(5f);
        bluePaint.setColor(Color.BLUE);
        bluePaint.setStyle(Paint.Style.STROKE);
        bluePaint.setStrokeJoin(Paint.Join.ROUND);


        black.setAntiAlias(true);
        black.setStrokeWidth(5f);
        black.setColor(Color.BLACK);
        black.setStyle(Paint.Style.STROKE);
        black.setStrokeJoin(Paint.Join.ROUND);




        //define bitmap
        redSquare = BitmapFactory.decodeResource(getResources(), R.drawable.red);
        blueSquare = BitmapFactory.decodeResource(getResources(), R.drawable.blue);


    }

    @Override
    public void onDraw(Canvas canvas){

        //canvas.drawPath(path, paint);
       canvas.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y,black);
       //Makes a straight line go through the center of the screen.
       canvas.drawLine(0,height/2.5f, width,height/2.5f,black);
       //Makes a straight line go up and down.
        canvas.drawLine(width/2.3f,0, width/2.3f, height,black);

         //if(red){
             for(Point point : redPointList){
                new red(redSquare,canvas,point.x, point.y);

             }
         //}
        //if(blue){
            for(Point pointt : bluePointList){
                  new blue(blueSquare,canvas,pointt.x, pointt.y);
            }
        //}







    }

    @Override
    public boolean onTouchEvent(final MotionEvent event){
        //Gets the coords of the tap

        redPoint.x = (int)  event.getX();
        redPoint.y = (int)  event.getY();



        bluePoint.x = (int) event.getX();
        bluePoint.y = (int)event.getY();

         eventX = event.getX();
         eventY = event.getY();




        int pointX = Math.round(eventX);
        int pointY = Math.round(eventY);





            //Shows the popup dialog when the user touches the screen
            AlertDialog.Builder alertThing = new AlertDialog.Builder(getContext());
            alertThing.setTitle("edit square");
            alertThing.setMessage("please choose an option");
            alertThing.setPositiveButton("red", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {









                    red = true;
                    blue = false;
                    invalidate();

                }
            });
            alertThing.setNeutralButton("Chnange color to blue", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {


                    bluePointList.add(bluePoint);



                    blue = true;
                    red = false;
                   invalidate();
                }
            });

            alertThing.create();
            alertThing.show();











        switch(event.getAction()){

            case MotionEvent.ACTION_DOWN:
                //Sets a new starting point
             pointStart.set(pointX,pointY);



               return true;

            case MotionEvent.ACTION_UP:
                //Contects the points
              pointEnd.set(pointX,pointY);
                break;




            default:
                return false;
        }
        invalidate();
        return true;
    }







}

最佳答案

您可以这样操作:

使您的MainDrawingView扩展ViewGroup而不是View并实现View.onClickListener

public class MainDrawingView extends ViewGroup implements View.onClickListener

在MainDrawingView中覆盖onClick
@Override public void onClick(View v) { //do stuff }

在MainActivity中创建一个按钮
并将其添加到您的

Button button = new Button(context);
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                               LayoutParams.WRAP_CONTENT));

gameView.addView(button);


在按钮上设置onClickListener

button.setOnClickListener(new MainDrawingView());

您可以在MainDrawingViewonClick方法中处理按钮的单击事件

10-07 13:54