如何结合OnClickListener和OnTouchListener一个的ImageButtonOnClickListener、OnTouchListener、ImageButton

2023-09-06 07:57:04 作者:不敢看这星空

我需要做一些事情,当用户点击的ImageButton 我试图创建一个静态类实现的两个 OnClickListener OnTouchListener

静态类ClickListenerForScrolling实现OnClickListener,OnTouchListener有以下方法:

  @覆盖
公共无效的onClick(视图v)
 

  @覆盖
公共布尔onTouch(查看为arg0,MotionEvent ARG1)
 

这背后的整个想法是改变的图像源的的ImageButton 当用户触摸它,当用户点击这个按钮执行任务。任何人都可以给我如何做这样的提示?

解决方案

  imageButton.setOnTouchListener(新View.OnClickListener(){

    公共无效onTouch(查看为arg0,MotionEvent ARG1){
        //改变背景在这里
    }
});

imageButton.setOnClickListener(新View.OnClickListener(){

    公共无效的onClick(查看为arg0){
        这里//行动
    }
});
 
setOnClickListener方法会出错

I need to do something when the user clicks the imageButton I've tried to create a static class that implements both OnClickListener and OnTouchListener

static class ClickListenerForScrolling implements OnClickListener, OnTouchListener that has the following methods:

@Override
public void onClick(View v)

and

@Override
public boolean onTouch(View arg0, MotionEvent arg1)

The whole idea behind this is to change the image source of the ImageButton when the user touches it, and perform a task when the user clicks this button. Can anybody give me a hint on how to do so?

解决方案

imageButton.setOnTouchListener(new View.OnClickListener() {

    public void onTouch(View arg0, MotionEvent arg1) {
        //change background here
    }
});

imageButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        //action here
    }
});