今天做了一个activity的对话框,点击对话框外部不消失。结果是API11以上的才会消失
有一个函数可以控制this.setFinishOnTouchOutside(false);
或者
<resources>
<style name="Theme.SoundRecorder" parent="@android:style/Theme.Holo.DialogWhenLarge">
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
</resources>
如果api小于11
可以加一个函数,判断点击的位置是否在对话框以外,如果是直接finish即可
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN && isOutOfBounds(this, event)) {
finish();
return true;
}
return super.onTouchEvent(event);
}
private boolean isOutOfBounds(Activity context, MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
final View decorView = context.getWindow().getDecorView();
return (x < -slop) || (y < -slop)|| (x > (decorView.getWidth() + slop))|| (y > (decorView.getHeight() + slop));
}