单击整个屏幕

问题描述 投票:0回答:2

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

我只看到我需要设置按钮的所有地方(没有按钮可能吗?),它可以是透明的,并且与屏幕具有相同的尺寸。是否还有其他可能性或OnClickListener仅在使用按钮时才有效?

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

screen onclicklistener
2个回答
0
投票

如果你在android(它听起来像)转到xml视图代码并添加android:onClick =“你点击时调用的方法”。转到活动代码并添加分配给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"

    >

然后在活动代码中:

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
}
}

0
投票

这是我做的:

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

<android.support.constraint.ConstraintLayout

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

//Other layouts

>

2)然后我在实现View.OnTouchListener之后在我的活动中添加它:

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

@Override

public boolean onTouch(View view, MotionEvent motionEvent) {

//DO THINGS


return false;

}

© www.soinside.com 2019 - 2024. All rights reserved.