Tap outside Android dialog to dismiss it?

前端 未结 12 1374
梦如初夏
梦如初夏 2020-12-07 18:44

I was wondering if it\'s possible to somehow tap outside a popup dialog (or an Activity with a dialog theme), and dismiss it by just tapping outside of it?

I made a

相关标签:
12条回答
  • 2020-12-07 19:29
    dialog.setCanceledOnTouchOutside(true) 
    

    Sets whether this dialog is canceled when touched outside the window's bounds.

    0 讨论(0)
  • 2020-12-07 19:35

    Old question but yet another solution:

    1. Make your foreground activity full-screen. Usenested layouts: The full-screen layout should have transparent background (e.g. @null or @android:color/transparent). The inner layout should have a visible background.

    2. Add an OnClickListener to the invisible outer layout that finish()es your activity.

    0 讨论(0)
  • 2020-12-07 19:35

    Use style of dialog rather than other styles.

    For example, Use

    public YourCustomDialog(Context context) {
        super(context, android.R.style.Theme_Holo_dialog_NoActionBar);
    }
    

    When you use other styles like Theme_Translucent_NoTitleBar , the dialog will not be dismissed.

    0 讨论(0)
  • 2020-12-07 19:37

    There is a TouchInterceptor method which will called when you touch on out side of popup window

    For example

    mWindow.setTouchInterceptor(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                        mWindow.dismiss();
    
                        return true;
                    }
    
                    return false;
                }
            });
    

    mWindow is the popup window

    And if you want same functionality for Activity you have to follow below steps.

    1) Add flag before setContentView() method called in onCreate();

     getWindow().setFlags(LayoutParams.FLAG_NOT_TOUCH_MODAL, LayoutParams.FLAG_NOT_TOUCH_MODAL);
    
        // ...but notify us that it happened.
        getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
    

    2) Override onTouchEvent() event in Activity

    and write below code

     @Override
            public boolean onTouchEvent(MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    Toast.makeText(getApplicationContext(), "Finish", 3000).show();
                    finish();               
                    return true;
                }
                return false;
            }
    

    The complete copy is here

    Activity

    package net.londatiga.android;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnTouchListener;
    import android.view.WindowManager.LayoutParams;
    
    import android.widget.Button;
    import android.widget.Toast;
    
    public class NewQuickAction3DActivity extends Activity implements OnTouchListener {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             // Make us non-modal, so that others can receive touch events.
            getWindow().setFlags(LayoutParams.FLAG_NOT_TOUCH_MODAL, LayoutParams.FLAG_NOT_TOUCH_MODAL);
    
            // ...but notify us that it happened.
            getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
    
            setContentView(R.layout.main);
    
        }
    
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                Toast.makeText(getApplicationContext(), "Hi", 3000).show();
    
                return true;
            }
    
            return false;
        }
    }
    

    This is manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="net.londatiga.android"
          android:versionCode="1"
          android:versionName="1.0">
        <uses-sdk android:minSdkVersion="7" />
    
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".NewQuickAction3DActivity"
                      android:label="@string/app_name" android:theme="@android:style/Theme.Holo.Dialog">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
        </application>
    </manifest>
    
    0 讨论(0)
  • 2020-12-07 19:37
    LayoutParams lp=dialogp.getWindow().getAttributes(); 
    lp.flags=LayoutParams.FLAG_LAYOUT_NO_LIMITS;
    

    I added this and it works flawlessly on 3.0 up, but should work on all.

    0 讨论(0)
  • 2020-12-07 19:44

    Any views within the dialog can set to consume the touch event so that below won't be called.

    onCreate(){
        getWindow().getDecorView().getRootView().setOnTouchListener(new OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                dialog.dismiss();
                return false;
            }
        });
    
    0 讨论(0)
提交回复
热议问题