Android应用不会进入onTouch方法

时间:2015-10-06 00:03:13

标签: android ontouchlistener ontouchevent ontouch

我在onTouch()方法上遇到了麻烦。我用来实现它的代码如下。

public class EjemploView extends View implements OnTouchListener  {
...
@Override
    public boolean onTouch(View v, MotionEvent event) {
            if(event.getActionMasked()==0) {
                slider2.setCenX(slider2.getCenX() - 1);
            }
        return false;
    }

}

关键是我在onTouch方法中设置了一个调试器停止,即使我触摸屏幕也不会进入它。我究竟做错了什么?

3 个答案:

答案 0 :(得分:0)

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    setContentView(new EjemploView(this));

这是我实例化视图的地方

public class EjemploView extends View implements OnTouchListener  {

    private Drawable drawableFader, drawableSlider;
    private Grafico fader1, fader2, slider1, slider2;

    public EjemploView(Context context) {
        super(context);
        Resources res = context.getResources();
        drawableFader = res.getDrawable(R.drawable.fader);
        drawableSlider=res.getDrawable(R.drawable.slider);

        fader1 = new Grafico(this, drawableFader);
        fader2 = new Grafico(this, drawableFader);
        slider1 = new Grafico(this, drawableSlider);
        slider2 = new Grafico(this, drawableSlider);


        fader1.setAlto(350);
        fader1.setAncho(64);
        fader2.setAlto(350);
        fader2.setAncho(64);
        fader2.setAngulo(90);


        slider1.setAlto(90);
        slider1.setAncho(55);
        slider2.setAlto(90);
        slider2.setAncho(55);
        slider2.setAngulo(90);


    }

    @Override
    protected void onSizeChanged(int ancho, int alto, int ancho_anter, int alto_anter) {
        super.onSizeChanged(ancho, alto, ancho_anter, alto_anter);
        // Una vez que conocemos nuestro ancho y alto.

        fader1.setCenX(ancho / 4 - 31);
        fader1.setCenY(alto / 4 - 50);
        slider1.setCenX(ancho / 4 - 28);
        slider1.setCenY(alto / 4 - 50);
        fader2.setCenX(ancho - 271);
        fader2.setCenY(alto / 4 - 50);
        slider2.setCenX(ancho - 277);
        slider2.setCenY(alto / 4 - 50);
    }

    @Override
    synchronized protected void onDraw(Canvas canvas) {
        fader1.dibujaGrafico(canvas);
        fader2.dibujaGrafico(canvas);
        slider1.dibujaGrafico(canvas);
        slider2.dibujaGrafico(canvas);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
            Log.d("TAG", "I've reached this point");
            if(event.getActionMasked()==0) {
                slider2.setCenX(slider2.getCenX() - 2);
            }
        return false;
    }
}

这是我的整个视图代码。它显示它是因为我可以看到drawables。

答案 1 :(得分:0)

您正在实施新界面,但您应该使用已定义的onTouchEvent(MotionEvent event)。只需覆盖它。

class CustomImageView extends ImageView {

    ...

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getActionMasked();

        Log.d(TAG, String.valueOf(action));

        return false;
    }
}

答案 2 :(得分:0)

添加作为答案,只是为了让它更有意义。 您正在实现onTouchListener,但您没有将其设置为onTouchListener。因此,您在视图中留下了一个功能完备的onTouchListener,它不会被调用。

public EjemploView(Context context) {
        super(context);
        /** \/ Add this line right here \/ */
        setOnTouchListener(this);
        Resources res = context.getResources();
        drawableFader = res.getDrawable(R.drawable.fader);
        drawableSlider=res.getDrawable(R.drawable.slider);

        fader1 = new Grafico(this, drawableFader);
        fader2 = new Grafico(this, drawableFader);
        slider1 = new Grafico(this, drawableSlider);
        slider2 = new Grafico(this, drawableSlider);


        fader1.setAlto(350);
        fader1.setAncho(64);
        fader2.setAlto(350);
        fader2.setAncho(64);
        fader2.setAngulo(90);


        slider1.setAlto(90);
        slider1.setAncho(55);
        slider2.setAlto(90);
        slider2.setAncho(55);
        slider2.setAngulo(90);


    }