setOnTouchListener() is giving me an error

前端 未结 7 530
北恋
北恋 2021-01-27 18:25
button.setOnTouchListener(new OnTouchListener()
{
  public void onClick(View v)
  {
      Toast.makeText(MainActivity.this, \"YOUR TEXT\", 5000).show();
  }
});
<         


        
7条回答
  •  情话喂你
    2021-01-27 19:26

    The code

    public void onClick(View v)
      {
          Toast.makeText(MainActivity.this, "YOUR TEXT", 5000).show();
      }
    

    is not valid for an onTouchListener. That is why you are getting the error, you should be using

    @Override
    public void onTouch(View v, MotionEvent e)
          {
              Toast.makeText(MainActivity.this, "YOUR TEXT", 5000).show();
          }
    

    instead if you really want an onTouchListener, although I highly suggest Chiral Code's suggestion of using an onClickListener

提交回复
热议问题