在API11之后添加了setFinishOnTouchOutside()方法,只需要在onCreate()方法中调用:
this.setFinishOnTouchOutside(false);
API11之前
public class MyActivity extends Activity { @Override protected 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); // Note that flag changes must happen *before* the content view is set. setContentView(R.layout.my_dialog_view); } @Override public boolean onTouchEvent(MotionEvent event) { // If we've received a touch notification that the user has touched // outside the app, finish the activity. if (MotionEvent.ACTION_OUTSIDE == event.getAction()) { finish(); return true; } // Delegate everything else to Activity. return super.onTouchEvent(event); } }
http://www.cnblogs.com/lcyty/p/3426946.html