Get the co-ordinates of a touch event on Android

前端 未结 4 570
眼角桃花
眼角桃花 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:36

    The corrected version without the errors:

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.TextView;
    public class MainActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            final TextView textView = (TextView) findViewById(R.id.textView);
            // this is the view on which you will listen for touch events
            final View touchView = findViewById(R.id.touchView);
            touchView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    textView.setText("Touch coordinates : "
                            + String.valueOf(event.getX()) + "x"
                            + String.valueOf(event.getY()));
                    return true;
                }
            });
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    }
    

提交回复
热议问题