【问题标题】:activity as dialog in android活动作为android中的对话框
【发布时间】:2012-04-03 05:40:28
【问题描述】:

我想使用 activity as dialog 并且我将活动的主题设置为对话并且我成功了。 但 我的问题是当我在活动之外单击时 它会自动关闭并开始上一个活动..

我尝试了一种方法来制作透明的父布局,它看起来不像对话框..

我想使用此活动在我的应用程序中创建新帐户,因为它只有 3 个字段,因此在平板电脑中它看起来有很大的空间未使用...所以我想将活动用作对话框.....

请提前...!!! 示例将不胜感激..!!!!!!

【问题讨论】:

    标签: android android-activity dialog


    【解决方案1】:

    尝试以下属性

        this.setFinishOnTouchOutside(false);
    

    【讨论】:

      【解决方案2】:

      根据需要更改代码。

      谢谢

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@android:color/transparent"
              android:orientation="vertical"
              android:paddingBottom="20dp" >
              <RelativeLayout
                  android:id="@+id/RlayMain"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:layout_marginLeft="20dp"
                  android:layout_marginRight="30dp"
                  android:layout_marginTop="120dp"
                  android:background="#FFFFFF"
                  android:padding="10dp" >
                  <TextView
                      android:id="@+id/txtsignin"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_centerHorizontal="true"
                      android:text="SIGN IN"
                      android:textColor="#000000"
                      android:textSize="25sp" />
                  <EditText
                      android:id="@+id/edtUserName"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/txtsignin"
                      android:layout_marginTop="10dp"
                      android:layout_toRightOf="@+id/txtuser"
                      android:hint="USERNAME" />
                  <EditText
                      android:id="@+id/edtPassword"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/edtUserName"
                      android:layout_marginTop="10dp"
                      android:hint="PASSWORD"
                      android:inputType="textPassword" />
                  <Button
                      android:id="@+id/btnSignIn"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/edtPassword"
                      android:layout_marginTop="10dp"
                      android:paddingBottom="10dp"
                      android:paddingTop="10dp"
                      android:text="Sign In" >
                  </Button>
                  <Button
                      android:id="@+id/btnSignUp"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/btnSignIn"
                      android:layout_marginTop="10dp"
                      android:paddingBottom="10dp"
                      android:paddingTop="10dp"
                      android:text="Sign Up For Free!" >
                  </Button>
              </RelativeLayout>
          </RelativeLayout>
      

      【讨论】:

      • 我应该为那些使用它的人指出,虽然使用 sp 最适合测量字体大小,但 dp 是(通常)最好的对于诸如布局间距之类的东西,应该用于测量您的边距 - not sp.
      【解决方案3】:

      如果您还没有尝试过,那么这是实现活动为对话框的方法: 在您的清单文件中,将以下属性添加到您的活动中:

           <activity
              android:name=".MyActivityName"
              android:theme="@android:style/Theme.Dialog" />
      

      【讨论】:

      • 这是有效的.. 活动作为对话框工作,但是当我在该对话框之外单击时,它会自动关闭......
      • 尝试添加以下属性:android:configChanges="orientation" 当我在自己的应用程序中自己尝试时 - 当我在外面触摸时它不会关闭 dialogActivity
      【解决方案4】:

      关于在窗口外点击时避免关闭Activity的问题 从 API 11 正如 Vivek 所说,使用 this.setFinishOnTouchOutside(false);

      但对于以前的 API,请使用以下代码:

      @Override
      public boolean onTouchEvent(MotionEvent event) {
          if ( event.getAction() == MotionEvent.ACTION_DOWN &&  isOutOfBounds(this, event)){
              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));
      }
      

      【讨论】:

        【解决方案5】:

        将活动作为我定义的对话框开始:

        <activity android:theme="@android:style/Theme.Dialog" />
        

        现在当我startActivity() 时,它显示为一个对话框,并且父活动显示在它后面。我想要一个 Button,当单击 Dialog 时,它会被关闭,并且父 Activity 应该在不刷新页面的情况下显示。

        像我们通常创建的那样创建一个活动。

        同时检查CustomDialogActivity.java on android.com

        我认为您应该将活动创建为对话框,这很有帮助。

        这样,您可以为活动设置样式和主题。

        【讨论】:

          【解决方案6】:

          在移动设备和平板电脑中使用相同的 Activity。

          手机:-

          1. 转到 res -> 值。
          2. 打开 styles.xml 并添加以下主题设置。

          styles.xml :-

              <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
              <!-- Customize your theme here. -->
              <item name="colorPrimary">@color/colorPrimary</item>
              <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
              <item name="colorAccent">@color/colorAccent</item>
          </style>
          

          平板电脑:- 以对话框形式启动活动。

          1. 转到“res”。
          2. 创建新文件夹“values-sw720dp”。
          3. 新建styles.xml并添加以下主题设置。

          styles.xml :-

          <style name="AppTheme" parent="Theme.AppCompat.Light.Dialog">
              <item name="windowActionBar">false</item>
              <item name="windowNoTitle">true</item>
              <item name="colorPrimary">@color/primary_color</item>
              <item name="colorPrimaryDark">@color/primary_dark_color</item>
              <item name="android:textColorPrimary">@color/white</item>
              <item name="colorAccent">@color/material_green_800</item>
              <item name="colorButtonNormal">@color/material_green_800</item>
              <item name="windowFixedHeightMajor">800dp</item>
              <item name="windowFixedHeightMinor">800dp</item>
          </style>
          

          Manifest.xml :-

              <activity
              android:name=".LogInActivity"
              android:label="@string/title_activity_log_in"
              android:screenOrientation="portrait"
              android:theme="@style/AppTheme"            //set the theme activity
              android:windowSoftInputMode="adjustPan" >
              </activity>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-01-10
            • 1970-01-01
            相关资源
            最近更新 更多