Get the co-ordinates of a touch event on Android

前端 未结 4 567
眼角桃花
眼角桃花 2020-11-30 04:26

I\'m new to Android, I\'ve followed the hello world tutorial through and have a basic idea of what\'s going on. I\'m particularly interested in the touch screen of my T-Mob

4条回答
  •  甜味超标
    2020-11-30 04:32

    It sounds like you want to get touch events from the whole area of your layout for this particular test. Try attaching the touch listener to your parent view rather than a separate target view.

    This isn't a very common approach. In most cases you will want to listen for touch events in a specific child view and if you have other views in your layout that handle touch events (such as a button) they'll take priority over a parent view. See http://developer.android.com/guide/topics/ui/ui-events.html for more info about handling UI events.

    Layout (touch_viewer.xml):

    
    
        
    
    

    And in your activity:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.touch_viewer);
    
        final LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
        final TextView text = (TextView) findViewById(R.id.text);
        parent.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent ev) {
                text.setText("Touch at " + ev.getX() + ", " + ev.getY());
                return true;
            }
        });
    }
    

提交回复
热议问题