如何在对话框打开时为活动设置可取消的false?

时间:2013-10-07 07:57:06

标签: android themes android-theme

嗨我做了一个n活动,因为我在另一个活动上打开一个活动作为对话框弹出窗口,所以问题就是我的主题,

 <item `name="android:windowCloseOnTouchOutside">@bool/config_closeDialogWhenTouchOutside</item>`

但我不知道我应该在"windowCloseOnTouchOutside"内更改什么值,以便我可以将我的活动对话框设置为可取消的错误..? 我在主题中的代码如下:

 <style name="Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
        <item name="android:windowBackground">@android:drawable/panel_background</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:windowCloseOnTouchOutside">@bool/config_closeDialogWhenTouchOutside</item>
        <item name="android:windowActionModeOverlay">true</item>

        <item name="android:colorBackgroundCacheHint">@null</item>

        <item name="textAppearance">@android:style/TextAppearance</item>
        <item name="textAppearanceInverse">@android:style/TextAppearance.Inverse</item>

        <item name="textColorPrimary">@android:color/primary_text_dark</item>
        <item name="textColorSecondary">@android:color/secondary_text_dark</item>
        <item name="textColorTertiary">@android:color/tertiary_text_dark</item>
        <item name="textColorPrimaryInverse">@android:color/primary_text_light</item>
        <item name="textColorSecondaryInverse">@android:color/secondary_text_light</item>
        <item name="textColorTertiaryInverse">@android:color/tertiary_text_light</item>
        <item name="textColorPrimaryDisableOnly">@android:color/primary_text_dark_disable_only</item>
        <item name="textColorPrimaryInverseDisableOnly">@android:color/primary_text_light_disable_only</item>
        <item name="textColorPrimaryNoDisable">@android:color/primary_text_dark_nodisable</item>
        <item name="textColorSecondaryNoDisable">@android:color/secondary_text_dark_nodisable</item>
        <item name="textColorPrimaryInverseNoDisable">@android:color/primary_text_light_nodisable</item>
        <item name="textColorSecondaryInverseNoDisable">@android:color/secondary_text_light_nodisable</item>
        <item name="textColorHint">@android:color/hint_foreground_dark</item>
        <item name="textColorHintInverse">@android:color/hint_foreground_light</item>
        <item name="textColorSearchUrl">@android:color/search_url_text</item>

        <item name="textAppearanceLarge">@android:style/TextAppearance.Large</item>
        <item name="textAppearanceMedium">@android:style/TextAppearance.Medium</item>
        <item name="textAppearanceSmall">@android:style/TextAppearance.Small</item>
        <item name="textAppearanceLargeInverse">@android:style/TextAppearance.Large.Inverse</item>
        <item name="textAppearanceMediumInverse">@android:style/TextAppearance.Medium.Inverse</item>
        <item name="textAppearanceSmallInverse">@android:style/TextAppearance.Small.Inverse</item>

        <item name="listPreferredItemPaddingLeft">10dip</item>
        <item name="listPreferredItemPaddingRight">10dip</item>
        <item name="listPreferredItemPaddingStart">10dip</item>
        <item name="listPreferredItemPaddingEnd">10dip</item>

        <item name="preferencePanelStyle">@style/PreferencePanel.Dialog</item>
    </style>

3 个答案:

答案 0 :(得分:36)

您可以非常轻松地在Java编码中实现这一点。 如果您有活动(即使它看起来像对话框),那么您应该

this.setFinishOnTouchOutside(false);

如果你使用过Dialog类,你应该调用

dialog.setFinishOnTouchOutside(false) 

如果您想在单击后台活动时阻止关闭它。

答案 1 :(得分:1)

这可以帮到你。这是一种处理外部触摸事件的方法:

How to cancel an Dialog themed like Activity when touched outside the window?

有一种方法可以从主题为对话框的活动中获得类似对话框的“触摸外部取消”行为,但我还没有完全调查它是否有不良副作用。

在您的Activity的onCreate()方法中,在创建视图之前,您将在窗口上设置两个标志:一个用于使其“非模态”,以允许您的活动视图以外的视图接收事件。第二个是接收其中一个事件已发生的通知,这将向您发送ACTION_OUTSIDE移动事件。

尝试如下:

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);
  }
}

答案 2 :(得分:1)

最好的方法是这段代码:

AlertDialog.Builder alert = new AlertDialog.Builder(Activity.this);
alert.setCancelable(false);