我正在尝试创建Button的子类。代码如下

package com.comnet.bookshelf;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Button;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;

public class myButton extends Button implements OnClickListener{
    Paint mainPaint;
    int colorArray[];
    int colorArrayAlt[];
    boolean touch=false;
    Rect bound;
    public myButton(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        mainPaint=new Paint();
        bound=new Rect();
        defaultColorIndexArray();
    }

    public myButton(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mainPaint=new Paint();
        bound=new Rect();
        defaultColorIndexArray();
    }

    public myButton(Context context)
    {
        super(context);
        mainPaint=new Paint();
        bound=new Rect();
        defaultColorIndexArray();
    }
    private void defaultColorIndexArray(){
        colorArray=new int[]{
        0xFFd28181, //1
        0xFFd48787,     //2
        0xFFd68d8d, //3
        0xFFd89292, //4
        0xFFda9898, //5
        0xFFdc9e9e, //6
        0XFFdea2a2, //7
        0XFFe0a8a8,     //8
        0xFFe2adad, //9
        0XFFe3b1b1, //10
        0XFFe4b4b4, //11
        0XFFe6b9b9, //12
        0XFFe7bdbd, //13
        0XFFe9c0c0, //14
        0XFFeac3c3, //15
        0XFFebc6c6, //16
        0XFFebc7c7, //17
        0XFFecc9c9, //18
        0XFFeccaca, //19
        0XFFeccaca,     //20
        0XFFeccbcb, //21
        0XFFeccbcb      //22
        };
        colorArrayAlt=new int[]{
        0XFFeccbcb, //22
        0XFFeccbcb, //21
        0XFFeccaca, //20
        0XFFeccaca, //19
        0XFFecc9c9, //18
        0XFFebc7c7, //17
        0XFFebc6c6, //16
        0XFFeac3c3, //15
        0XFFe9c0c0, //14
        0XFFe7bdbd, //13
        0XFFe6b9b9, //12
        0XFFe4b4b4, //11
        0XFFe3b1b1, //10
        0xFFe2adad, //9
        0XFFe0a8a8, //8
        0XFFdea2a2, //7
        0xFFdc9e9e, //6
        0xFFda9898, //5
        0xFFd89292, //4
        0xFFd68d8d, //3
        0xFFd48787, //2
        0xFFd28181  //1
        };
    }
    public void setButtonColorIndexArray(int carray[], int carray_alt[]){
    }
    @Override
    protected void onDraw(Canvas canvasObject) {
        super.onDraw(canvasObject);
        int draw_color[];
        if(touch==false)
        draw_color=colorArray;
        else
        draw_color=colorArrayAlt;
        int width=canvasObject.getWidth();
        int height=canvasObject.getHeight();
        double colorcount=44;
        double divheight=height;
        double f=colorcount/divheight;

        double step=0;
        for(int i=0;i<height;i++,step+=f){
        int next=(int) Math.round(step);
        if(next>43) next=43;
        else if(next<0)next=0;
        if(next<22){
        mainPaint.setColor(draw_color[next]);
        canvasObject.drawLine(0, i, width, i, mainPaint);
        }
        else{
        mainPaint.setColor(draw_color[43-next]);
        canvasObject.drawLine(0, i, width, i, mainPaint);
        }
        //int color=;
        mainPaint.setColor(getCurrentTextColor());

        String str=this.getText().toString();
        //mainPaint.setColor(Color.WHITE);
        mainPaint.setStyle(Style.FILL);
        //canvasObject.drawPaint(mainPaint);

        mainPaint.setColor(Color.BLACK);
        mainPaint.setTextSize(20);
        mainPaint.getTextBounds(str, 0, str.length(), bound);
        int x=(width-(bound.right-bound.left))/2;
        int y=(height+(bound.bottom-bound.top))/2;

        canvasObject.drawText(str, x, y, mainPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev){
        if(ev.getAction()==MotionEvent.ACTION_DOWN){
        //Log.i("Click", "ACTION_DOWN");
        touch=true;
        this.invalidate();
        }
        else if(ev.getAction()==MotionEvent.ACTION_UP){
        touch=false;
        this.invalidate();
        }

        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i("Clicked", "Clicked");
    }
}


除一个以外的所有东西都按我的预期工作。

onClick完全没有响应。代码如下:

((myButton)inflatedLayout.findViewById(R.id.DeleteButton)).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        DeleteTheEntireBook();
    }
});


具有默认类别的按钮可以正常工作

如果您还有其他问题,请告诉我。

最佳答案

问题是您已覆盖onTouchEvent。


标记这些词:


  您应该在OnTouchListener中返回false,然后也会处理OnClickListener。


因此,您可能会想,我只会在那做我的事情,然后返回false,这样我也可以获得点击。如果这样做,它将起作用,但是您将不会订阅其他即将发生的触摸事件(ACTION_MOVE,ACTION_UP),因此唯一的选择是在此处返回true,但不会像我们之前所说的那样收到任何点击事件。

因此,您需要使用performClick()在ACTION_UP中手动执行点击

经过测试的工作代码:

@Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            // Log.i("Click", "ACTION_DOWN");
            touch = true;
            this.invalidate();

        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            touch = false;
            this.invalidate();
            // KEY LINE
            performClick();
        }
        return true;


    }


希望对你有帮助

07-27 17:37