How do I detect touch input on the Android

前端 未结 5 1477
無奈伤痛
無奈伤痛 2020-12-05 11:18

Right now all I am trying to do is detect when the screen is pressed and then display a log message to confirm it happened. My code so far is modified off of the CameraPrevi

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 11:53

    I did it like this:

    public class ActivityWhatever extends Activity implements OnTouchListener
    {
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.yourlayout);
    
            //the whole screen becomes sensitive to touch
            mLinearLayoutMain = (LinearLayout) findViewById(R.id.layout_main);
            mLinearLayoutMain.setOnTouchListener(this);
        }
    
        public boolean onTouch(View v, MotionEvent event)
        {
            // TODO put code in here
    
            return false;//false indicates the event is not consumed
        }
    }
    

    in the xml of your view, specify:

    
    
    
    
        
    
    
    

提交回复
热议问题