android - 我可以在不使用监听器的情况下获取触摸坐标吗?

标签 android

有没有办法在不使用 OnTouchListener 事件的情况下获取触摸坐标(当用户触摸/多点触摸 Activity 时)?如果可能,请提供任何示例。

最佳答案

试试这段代码:

int x, y;
TextView tv, tl;
FrameLayout fl;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    tv = (TextView) findViewById(R.id.textView);
    tv.setText("X Coordinate");

    tl = (TextView) findViewById(R.id.touchView);
    tl.setText("Y Coordinate");
}

@Override
public boolean onTouchEvent(MotionEvent e) {
    x = (int) e.getX();
    y = (int) e.getY();
    tv.setText("" + x);
    tl.setText("" + y);

    return true;
}

关于android - 我可以在不使用监听器的情况下获取触摸坐标吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6591805/

相关文章:

android - Compose 布局垂直对齐 : SpaceBetween having no impact

java - android:为什么要关闭 SQLite 数据库游标,尽管它们会在它们的 finalize 方法中关闭?

Android 通过什么在 Facebook 上分享?

java - 错误 :Execution failed for task ':app:transformClassesWithFirebasePerformancePluginForRelease'

Android程序类型已经存在错误

java - 根据我当前的位置每秒更新 TextView

android - 从 Android 上的 OPUS 帧创建可播放的音频文件。

android - RadioGroup 按钮图像被截断

android - 在移动设备上更新 Tensorflow Lite 模型而不更新应用程序?

android - Firebase 子监听器未显示任何数据