0

Situation: i have this button (image 1) that launches the activity (image 2).

Question: how can i make it to where when the user clicks the background (transparent) on the second image it will go back to the first screen?

enter image description here

enter image description here

class with button: package com.projectcaruso.naturalfamilyplaning;

    import com.projectcaruso.naturalfamilyplaning.R;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentTransaction;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.ImageButton;

    public class LoginFragment extends Fragment implements OnClickListener{

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }         

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_login, null);

            ImageButton connect = (ImageButton) view.findViewById(R.id.facebooklogin);
            connect.setOnClickListener(this);


            return view;
        }

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            // Create new fragment and transaction
            Fragment newFragment = new FacebookActivity();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();

            Intent intent = new Intent();
            intent.setClass(getActivity(), FacebookActivity.class);
             startActivity(intent);
        }

    }

DialogFragment : package com.projectcaruso.naturalfamilyplaning;

import com.projectcaruso.naturalfamilyplaning.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;

public class FacebookActivity extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.facebook_login, null))

        // Add action buttons
               .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                       // sign in the user ...
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       FacebookActivity.this.getDialog().cancel();
                   }
               });      
        return builder.create();
    }
}

EDIT logcat:

    05-17 18:28:21.224: W/dalvikvm(27699): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
    05-17 18:28:21.245: E/AndroidRuntime(27699): FATAL EXCEPTION: main
    05-17 18:28:21.245: E/AndroidRuntime(27699): java.lang.IllegalArgumentException: No view found for id 0x7f050038 (com.projectcaruso.naturalfamilyplaning:id/fragment_container) for fragment FacebookActivity{40cd27e8 #2 id=0x7f050038}
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at android.os.Handler.handleCallback(Handler.java:725)
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at android.os.Handler.dispatchMessage(Handler.java:92)
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at android.os.Looper.loop(Looper.java:137)
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at android.app.ActivityThread.main(ActivityThread.java:5041)
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at java.lang.reflect.Method.invokeNative(Native Method)
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at java.lang.reflect.Method.invoke(Method.java:511)
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    05-17 18:28:21.245: E/AndroidRuntime(27699):    at dalvik.system.NativeStart.main(Native Method)
    05-17 18:28:23.764: E/Trace(27715): error opening trace file: No such file or directory (2)

EDIT XML:

    <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent" >

            <RelativeLayout 
                android:id="@+id/fragment_container"
                android:layout_width="250dp"
                android:layout_height="350dp"
                android:layout_gravity="center"
                android:background="#3B5998"
                android:orientation="vertical">


                        <!-- Facebook logo -->
                         <ImageView
                            android:id="@+id/facebooklogo3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignLeft="@+id/lblEmail"
                            android:layout_alignParentTop="true"
                            android:layout_alignRight="@+id/txtEmail"
                            android:layout_marginTop="19dp"
                            android:src="@drawable/com_facebook_logo" />

                         <!-- Email inforamtion -->
                         <TextView
                            android:id="@+id/lblEmail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignLeft="@+id/lblPassword"
                            android:layout_below="@+id/facebooklogo3"
                            android:text="Email:"
                            android:textColor="#ffffff"
                            android:textSize="20sp" />

                        <EditText
                            android:id="@+id/txtEmail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignLeft="@+id/lblEmail"
                            android:layout_alignParentRight="true"
                            android:layout_below="@+id/lblEmail"
                            android:layout_marginRight="10dp"
                            android:background="#fff"
                            android:ems="10"
                            android:inputType="textEmailAddress" />

                        <!-- Passworld informaiton -->
                        <TextView
                            android:id="@+id/lblPassword"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/txtEmail"
                            android:layout_marginLeft="10dp"
                            android:text="Password:"
                            android:textColor="#ffffff"
                            android:textSize="20sp" />

                        <EditText
                            android:id="@+id/txtPassword"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignLeft="@+id/lblPassword"
                            android:layout_alignParentRight="true"
                            android:layout_below="@+id/lblPassword"
                            android:layout_marginRight="10dp"
                            android:background="#fff"
                            android:ems="10"
                            android:inputType="textPassword" 
                            android:padding="2dp"/>

                        <!-- Save -->

                        <TextView
                            android:id="@+id/signup"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentBottom="true"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentRight="true"
                            android:layout_marginBottom="20dp"
                            android:gravity="center"
                            android:text="Need an account? \n Sign up using your phone"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="#ffffff" />

                        <CheckBox
                            android:id="@+id/savelogin"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignLeft="@+id/txtPassword"
                            android:layout_below="@+id/txtPassword"
                            android:layout_marginTop="16dp"
                            android:text="Save Login"
                            android:textColor="#ffffff"
                            android:textSize="20sp" />
                </RelativeLayout>
    </RelativeLayout>

manafest:

    <activity android:name=".FacebookActivity" android:theme="@style/Theme.Transparent"/>
4

2 回答 2

1

您可以将第二个屏幕实现为Dialog然后调用dialog.setCanceledOnTouchOutside(true),如果您在对话框之外触摸屏幕,它将关闭对话框。

于 2013-05-17T14:18:39.617 回答
0

您可以按照 makovkastar 所述进行操作,或者不太理想的解决方案是覆盖活动的默认 onTouch 方法并检测用户按下的位置。如果它在盒子的边界之外,将它的可见性属性发送到View.GONE

于 2013-05-17T14:30:38.957 回答