按钮子类onClick没有响应

时间:2015-02-02 08:52:16

标签: java android

我正在尝试创建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();
    }
}); 

默认类的按钮工作正常

如果您有任何疑问,请与我联系。

3 个答案:

答案 0 :(得分:2)

  

问题是你已经覆盖了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;


    }

希望它有助于ツ

答案 1 :(得分:0)

在构造函数中,添加onClickListener, 而且不需要((myButton)inflatedLayout.findViewB...

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

然后,点击按钮后,应调用onClick类中被覆盖的myButton

答案 2 :(得分:0)

View.OnClickListener:单击视图时要调用的回调的接口定义。 你能使用这个代码:

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

});