点击全屏

Click on the whole screen

我有一个项目,我需要在整个屏幕上设置一个点击监听器(当屏幕被点击时,我收集加速度计的值)。

只看到到处都需要设置一个按钮(没有按钮不行吗?)可以是透明的,和屏幕尺寸一样。是否有任何其他可能性或仅在使用按钮时 OnClickListener 才有效?

我希望我已经足够理解了。

如果您在 android(听起来像),请转到 xml 查看代码并添加 android:onClick="your method to be called when clicked"。转到 activity 代码并添加您分配给 onClick 属性的方法。

xml查看代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout"
    android:background="@drawable/tempback"
    android:onClick="clickListener"

    >

然后在activity代码中:

public void clickListener(View v){
  if(v.getId() == R.id.idOfViewWithClickListener){ //Thismight be optional
      //What you want to happen when the view has been clicked
}
}

这是我所做的:

1) 在我的 xml 查看代码中,我检查了包括所有其他布局的名称 "ConstraintLayout"

<android.support.constraint.ConstraintLayout

xmlns:android="http://schemas.android.com/apk/res/android"

//Other layouts

>

2) 然后我在实施 View.OnTouchListener 后将其添加到我的 activity 中:

((ConstraintLayout) findViewById(R.id.layout_main)).setOnTouchListener(this);

@Override

public boolean onTouch(View view, MotionEvent motionEvent) {

//DO THINGS


return false;

}