How do I detect touch input on the Android

前端 未结 5 1478
無奈伤痛
無奈伤痛 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:

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/layout_main">
    
        <!-- other widgets go here-->
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-05 11:55

    Here is a simple example on how to detect a simple on touch event, get coords and show a toast. The event in this exmple are Action Down, Move and Action up.

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        private boolean isTouch = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
    
            int X = (int) event.getX();
            int Y = (int) event.getY();
            int eventaction = event.getAction();
    
            switch (eventaction) {
                case MotionEvent.ACTION_DOWN:
                    Toast.makeText(this, "ACTION_DOWN AT COORDS "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
                    isTouch = true;
                    break;
    
                case MotionEvent.ACTION_MOVE:
                    Toast.makeText(this, "MOVE "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
                    break;
    
                case MotionEvent.ACTION_UP:
                    Toast.makeText(this, "ACTION_UP "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
                    break;
            }
            return true;
        }
    }
    
    0 讨论(0)
  • 2020-12-05 11:57

    //on finger touch view visible. On finger up gone

        hintView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
              if(event.getAction()==MotionEvent.ACTION_MOVE){
                    hintText.setVisibility(View.VISIBLE);
                }else if(event.getAction()==MotionEvent.ACTION_UP){
                  hintText.setVisibility(View.GONE);
    
                }
    
                return true;
            }
        });
    
    0 讨论(0)
  • 2020-12-05 12:01

    Try code below to detect touch events.

    mView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //show dialog here
            return false;
        }
    });
    

    To show dialog use Activity method showDialog(int). You have to implement onCreateDialog(). See documentation for details.

    0 讨论(0)
  • 2020-12-05 12:14

    I have tried alot and finally found a solution to detect touch in a screen after 2 days.

    Kotlin:

    If you are having bottom navigation bar and you want to hide on touch...try this!

    Activity.dispatchTouchEvent(MotionEvent) - This allows your Activity to intercept all touch events before they are dispatched to the window.

      override fun dispatchTouchEvent(event: MotionEvent): Boolean {
    
       if (event.getAction() === MotionEvent.ACTION_DOWN) {
           if (event.getAction() === MotionEvent.ACTION_DOWN) {
    
    
    
    
           }
       } else if (event.getAction() === MotionEvent.ACTION_MOVE) {
           tabLayout.visibility = View.GONE
           tv_chat.visibility = View.GONE
    
    
    
       } else if (event.getAction() === MotionEvent.ACTION_UP) {
    
           tabLayout.visibility = View.VISIBLE
           tv_chat.visibility = View.VISIBLE
    
       }
        return super.dispatchTouchEvent(event)
    }
    
    0 讨论(0)
提交回复
热议问题