2012-04-03 36 views
8

Tôi muốn sử dụng activity as dialog và tôi đã tạo chủ đề hoạt động làm hộp thoại & tôi thành công. nhưng tôi có vấn đề ở đây là khi tôi nhấp vào bên ngoài của hoạt động tự động được đóng & các hoạt động trước đó của nó bắt đầu ..hoạt động dưới dạng hộp thoại trong android

tôi đã cố gắng một điều để làm cho bố trí cha mẹ trong suốt nó không giống như một hộp thoại.

tôi muốn sử dụng hoạt động này để tạo tài khoản mới trong ứng dụng vì nó chỉ có 3 trường để trong máy tính bảng có vẻ không gian lớn không sử dụng ... vì vậy tôi muốn sử dụng hoạt động làm hộp thoại .....

thenx trước ... !!! ví dụ sẽ được đánh giá cao .. !!!!!

Trả lời

18

thử với sau tài sản

this.setFinishOnTouchOutside(false); 
+0

thenx anh trai ... làm việc của nó .... – Wolverine

+0

này đang làm việc tốt –

2

để Bắt đầu hoạt động như thoại tôi định nghĩa

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

bây giờ khi là tartActivity() nó hiển thị như hiển thị hộp thoại và hoạt động cha mẹ trên lưng, i wan một nút cho ai tôi nhấp vào hộp thoại sẽ bỏ qua và hoạt động gốc sẽ hiển thị mà không cần làm mới trang.

Tạo hoạt động như thường chúng tôi tạo ra.

cũng kiểm tra CustomDialogActivity.java on android.com

tôi nghĩ rằng u nên tạo hoạt động như một dialog.then nó giúp

u có thể thiết lập phong cách và chủ đề cho hoạt động ur của thành viên này ..

3

nếu bạn chưa có đã thử nó, sau đó đây là cách để đạt được hoạt động như hộp thoại: trong tệp kê khai của bạn, thêm vào hoạt động của bạn thuộc tính sau:

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

này đang làm việc .. hoạt động như hộp thoại nhưng khi tôi nhấp vào bên ngoài hộp thoại của nó sẽ tự động đóng lại ...... – Wolverine

+0

hãy thử thêm thuộc tính sau: android: configChanges = "orientation" khi tôi cố gắng tự nó trong ứng dụng của riêng tôi - nó không đóng hộp thoạiKích hoạt khi tôi đang chạm vào bên ngoài –

7

Thực hiện thay đổi mã theo nhu cầu của bạn.

Cảm ơn

<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

Cảm ơn bạn vì điều này, có vẻ tuyệt vời. +1 – pandabear

2

Đối với vấn đề tránh đóng cửa hoạt động khi nhấp bên ngoài cửa sổ từ API 11 như đã đề cập bởi Vivek sử dụng this.setFinishOnTouchOutside (false);

nhưng đối với API trước khi sử dụng mã này:

@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

Sử dụng Hoạt động tương tự trong các thiết bị di động và viên nén.

thoại di động: -

  1. Chuyển đến res -> giá trị.
  2. Mở styles.xml và thêm các cài đặt chủ đề sau.

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: - Launch hoạt động như thoại.

  1. Goto "res".
  2. Tạo thư mục mới "values-sw720dp".
  3. Tạo tệp styles.xml mới và thêm các cài đặt chủ đề sau.

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> 
Các vấn đề liên quan