setOnTouchListener() is giving me an error

前端 未结 7 518
北恋
北恋 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:04

    Use this code:

        button.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "YOUR TEXT", 5000).show();
            }
        });
    
    0 讨论(0)
  • 2021-01-27 19:08

    you are putting an ontouchlistener with an onclick listener thats your mistake.

        button.setOnTouchListener(new OnTouchListener(){
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
    
         Toast.makeText(MainActivity.this, "YOUR TEXT", 5000).show();
        return false;
        }
     });
    
    0 讨论(0)
  • 2021-01-27 19:09

    I also had this problem and I had to do 'fix project setup' which I found after clicking the quick fixes option. I then added a .jar file and it was all good.

    0 讨论(0)
  • 2021-01-27 19:11

    and when u use new OnTouchListener() u need to override on onTouch not onClick or change it to
    plus.setOnClickListener(new OnClickListener()

     @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
    
                Toast.makeText(MainActivity.this, "YOUR TEXT", 5000).show();
    
                return false;
            }
    
    0 讨论(0)
  • 2021-01-27 19:15

    Check your code: http://developer.android.com/reference/android/view/View.OnTouchListener.html You should use method onTouch.

    0 讨论(0)
  • 2021-01-27 19:16

    There are two solutions:

    1. Add import android.view.View.OnTouchListener; to the beginning of your class

    2. Replace button.setOnTouchListener(new OnTouchListener() with button.setOnTouchListener(new ViewOnTouchListener() as Chiral Code suggested.

    0 讨论(0)
提交回复
热议问题