代码之家  ›  专栏  ›  技术社区  ›  user9357066

单击整个屏幕

  •  0
  • user9357066  · 技术社区  · 6 年前

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

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

    我希望我能被理解。

    2 回复  |  直到 6 年前
        1
  •  0
  •   No Co    6 年前

    如果您使用的是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
    }
    }
    
        2
  •  0
  •   user9357066 user9357066    6 年前

    以下是我所做的:

    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;
    

    }