how to fire an event when someone clicks anywhere on the screen in an android app?

前端 未结 7 522
暗喜
暗喜 2020-12-29 03:06

how can I catch the event when click occurs somewhere on the app screen? It doesn\'t matter if there is a button or something else. I just need to apply an onClick()

相关标签:
7条回答
  • 2020-12-29 03:44

    setonclicklistner for main layout of your layout file....

    Like....main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/mainlayout">
     <!- Your other view->
    </Relativelayout>
    

    and set click listener for mainlayout...

     RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.mainlayout);
     rlayout.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
    
        }
    
     });
    
    0 讨论(0)
  • 2020-12-29 03:52

    I believe the easiest way is to override onUserInteraction in your Activity

    Just be aware that it won't fire when users focus on/off EditTexts and Spinners (probably other Widgets too). But it will fire every time the user touches the screen or presses a Button.

    But this makes it unnecessary to build listeners into your layouts or write additional methods to handle those listeners, so I believe it's the most trouble-free way to get what you want.

    0 讨论(0)
  • 2020-12-29 03:58

    You should just override Activity's dispatchTouchEvent(ev: MotionEvent) method


    Look at this example in Kotlin. This approach will not affect any onClickListenters in your app

    /**
     * On each touch event:
     * Check is [snackbar] present and displayed
     * and dismiss it if user touched anywhere outside it's bounds
     */
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        // dismiss shown snackbar if user tapped anywhere outside snackbar
        snackbar?.takeIf { it.isShown }?.run {
            val touchPoint = Point(Math.round(ev.rawX), Math.round(ev.rawY))
            if (!isPointInsideViewBounds(view, touchPoint)) {
                dismiss()
                snackbar = null // set snackbar to null to prevent this block being executed twice
            }
        }
    
        // call super
        return super.dispatchTouchEvent(ev)
    }
    

    Or check out the full gist

    0 讨论(0)
  • 2020-12-29 03:59

    You can also directly set a callback from the onClick xml attribute of the root layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:onClick"myMethod">
     <!- Your other view->
    </Relativelayout>
    

    And in your activity:

    public void myMethod(View pView) {
      //XXX do something
    }
    

    Less bloated code this way :)

    0 讨论(0)
  • 2020-12-29 04:05

    Maybe you can add a listener to the Layout instance, getting the layout with

    RelativeLayout l = (RelativeLayout)findViewById(R.id.mylayout);
    l.setOnClickListener(click);
    
    0 讨论(0)
  • 2020-12-29 04:07

    If you want to know, that the user clicked anywhere on the screen then the trick is to override OnUserUnteraction():

    "Called whenever a key, touch, or trackball event is dispatched to the activity.Implement this method if you wish to know that the user has interacted with the device in some way while your activity is running. This callback and {@link #onUserLeaveHint} are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notification...."

    public abstract class BaseActivity extends Activity {
    ...
    @Override
    public void onUserInteraction() {
        Log.d(DEBUG_TAG,"Touch anywhere happened");
        super.onUserInteraction();
    }
    

    I am using it to set up app screen block after some inactivity.

    0 讨论(0)
提交回复
热议问题