有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

实现OnClick和setOnClick方法的java Android类

我正在创建一个实现onclick的可重用类和一个设置onclick的方法。该类将表示在画布上绘制的“按钮”。我知道使用x和y位置来评估按钮是否按下

如果查看代码,您可以看到一个isPressed实现,它只返回这个值(这只适用于澄清)

我需要的是能够为画布上绘制的每个项目提供一种独特的行为,这就是为什么我要实现onclick侦听器

public class CanvasButton extends CanvasItem implements View.OnClickListener {
    private View.OnClickListener ClickListener;

    public CanvasButton(float x, float y, int width, int height){
        super(x, y, width, height);
    }

    public boolean isPressed(float x, float y){
        return isTouched(x, y);
    }

    public void setOnClickListener(View.OnClickListener listener){
        this.ClickListener = listener;
    }

    @Override
    public void onClick(View v) {
    }
}

我似乎不知道该怎么做

更新:我想我从提供的代码中给人的印象是错误的。我试着看风景。但那只是我认为我有的一个选择。任何行为类似于上述onclick侦听器的方法都可以


共 (3) 个答案

  1. # 1 楼答案

    多亏了@mdtuyen's answer我才能够装配出适合我的东西。 我实现了一个基础CanvasItem类,现在可以继承它来实现这个技巧(我省略了不相关的类方法

    import android.view.View;
    
    public class CanvasItem { //notice does not need to implement onClickListener
    
        //this is used to get the click
        private View.OnClickListener onClickListener;
    
        protected CanvasItem(){
          /* contructor logic goes here */
          /* at minimum x and y and width and height is needed */
        }
    
        protected boolean isHit(float x, float y){
            /* left() right() top() and bottom() use widths and height from my constructor */
            /* x and y is passed from canvas ontouch */
            if(x >= left() && x <= right() && y >= top() && y <= bottom()){
    
                //fire any onclick listeners
                if(onClickListener != null){
                    onClickListener.onClick(null);
                }
    
                //return true
                return true;
            }
    
            //not touched
            return false;
        }
    
        protected void setOnTouchListener(View.OnClickListener clickListener){
            onClickListener = clickListener;
        }
    }
    

    实现后,我使用了CanvasItem的ArrayList或继承CanvasItem的类,该类运行以确定isHit()

    public boolean onTouchEvent(MotionEvent event) {
    
            final int action = event.getAction();
    
            float x = event.getX();
            float y = event.getY();
    
            switch (action & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    for(CanvasItem itm : mItems){
                         //if item istouched
                         if(itm.isHit(x, y)){
                            /* Logic can go here to alter any properties */
                         }
                     }
             }
        }
    

    一旦项目被删除,if语句中就不需要任何东西。激发isHit(),然后激发onClick

    是的,这实现了与正常操作类似的功能,但使用onClick I可以在触摸屏上执行特定操作,而无需管理和跟踪画布上的项目,这对于画布上可拖动的项目来说是一项更大的任务

  2. # 2 楼答案

    只能将onClick侦听器和onClick方法添加到视图中。画布和其中的任何东西都不是视图,不可单击。让画布内部有可点击按钮的唯一方法是将整个画布放在一个视图或一个扩展视图的类中,重写onTouch方法,并在该方法中获取触摸的位置,查看它们是否在任何按钮的边界内,以便运行一些代码。以下是一些其他问题的链接

    onClickListener in Canvas

    add touch event listener to android canvas

    public class MyCanvasView extends View implements OnTouchListener {
    
        Bitmap button1;
        Bitmap button2;
        Bitmap button3;
        Bitmap button4;
    
        public MyCanvasView(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);
            this.setOnTouchListener(this);
        }
    
        public void onDraw(Canvas canvas) {
            paint = new Paint();
            canvas.drawBitmap(button1, 10, 10, paint);
            canvas.drawBitmap(button2, 60, 10, paint);
            canvas.drawBitmap(button3, 110, 10, paint);
            canvas.drawBitmap(button4, 160, 10, paint);
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int x = event.getX();
            int y = event.getY();
            if (x >= 60 && x <= (60 + canvasButtonWidth)){
                if (y >= 10 && x <= (10 + canvasButtonHeight)) {
                    //Insert code you want to use when button2 is clicked
                }
            }
            /*Copy and paste the pair of if statements above for each button you have 
            /drawn on the canvas*/
            return true;
        }
    }
    

    想得到挖掘机的徽章

  3. # 3 楼答案

    OnClick需要有如下格式:

    @Override
    public void onClick(View v) {
        if (ClickListener != null){
            ClickListener.onClick(v);
        }
        //The code to reuse
    }