2012-04-03 28 views
8

Ich möchte eine activity as dialog verwenden und ich machte das Thema der Aktivität als Dialogfeld & ich erfolgreich. aber ich habe hier das Problem ist, wenn ich außerhalb der Aktivität klicken seine automatisch & der vorherigen Aktivität geschlossen loszulegen bekommen ..Aktivität als Dialog in Android

ich eine Sache versucht, transparent Eltern Layout machen es nicht wie ein Dialog sieht. .

Ich möchte diese Aktivität verwenden, um ein neues Konto in meiner Anwendung zu erstellen, da es nur 3 Felder hat, so dass es im Tablet großen Raum ungenutzt aussieht ... also möchte ich Aktivität als Dialog verwenden .....

thenx im voraus ... !!! Beispiele werden geschätzt .. !!!!!

Antwort

18

versuchen, mit folgenden Eigenschaft

this.setFinishOnTouchOutside(false); 
+0

thenx Bruder ... seine Arbeits .... – Wolverine

+0

dies funktioniert gut –

2

Aktivität als Dialog zu starten definiert i

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

jetzt wann ist tartActivity() es wie Dialog und Eltern Aktivitätsanzeige auf der Rückseite angezeigt werden, wan ich einen Knopf an wen Ich klicke auf den Dialog sollte ablehnen und die übergeordnete Aktivität sollte angezeigt werden, ohne die Seite zu aktualisieren.

Erstellen Sie eine Aktivität, wie wir sie normalerweise erstellen.

überprüfen auch CustomDialogActivity.java on android.com

ich denke, u Tätigkeit als dialog.then schaffen sollte es

hilft u Stil und Thema für die ur-Aktivität durch diese einstellen ..

3

wenn Sie nicht haben bereits versucht es, dann ist dies die Art und Weise Aktivität als Dialog zu erreichen ist: in Ihrer Manifest-Datei, das folgende Attribut auf Ihre Aktivität hinzufügen:

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

das funktioniert .. Aktivität arbeitete als Dialogfeld, aber wenn ich außerhalb dieses Dialogfeldes klicken wird automatisch geschlossen ...... – Wolverine

+0

versuchen, das folgende Attribut hinzufügen: android: configChanges = "Orientierung", wenn ich bin versuche es selbst in meiner eigenen Anwendung - es schließt die dialogActivity nicht, wenn ich mich außerhalb berühre –

7

Ändern Sie den Code entsprechend Ihren Anforderungen.

Dank

<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="20sp" > 
     <RelativeLayout 
      android:id="@+id/RlayMain" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="20sp" 
      android:layout_marginRight="30sp" 
      android:layout_marginTop="120sp" 
      android:background="#FFFFFF" 
      android:padding="10sp" > 
      <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="10sp" 
       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="10sp" 
       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="10sp" 
       android:paddingBottom="10sp" 
       android:paddingTop="10sp" 
       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="10sp" 
       android:paddingBottom="10sp" 
       android:paddingTop="10sp" 
       android:text="Sign Up For Free!" > 
      </Button> 
     </RelativeLayout> 
    </RelativeLayout> 
+1

Danke dafür, es sieht gut aus. +1 – pandabear

2

Für die Frage der Vermeidung der Aktivität schließen, wenn außerhalb des Fensters von API klicken 11 wie von Vivek this.setFinishOnTouchOutside (false) verwenden erwähnt;

aber für früheren APIs verwenden diesen Code:

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

Gleiche Aktivität unter Verwendung in mobilen Geräten und Tablets.

Mobil: -

  1. Goto res -> Werte.
  2. Öffnen Sie styles.xml und fügen Sie die folgenden Designeinstellungen hinzu.

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> 

Tablet: - Auflegungs Aktivität als Dialog.

  1. Gehe zu "res".
  2. Erstellen Sie einen neuen Ordner "values-sw720dp".
  3. Erstellen Sie neue styles.xml und fügen Sie folgende Designeinstellungen hinzu.

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> 
Verwandte Themen