2013-02-13 130 views
2

如果用戶登錄,我想在某個部分顯示某些內容。但是,如果用戶未登錄,我想顯示一個對話框提示用戶登錄。我也想在這個對話框中顯示facebook登錄按鈕。對話框中的Facebook登錄按鈕

但是,對話框中的登錄按鈕似乎不起作用。當我把它放在活動佈局中時,它確實有效。

有沒有辦法讓它在對話框中正常工作?

這裏是對話框XML:

<?xml version="1.0" encoding="UTF-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/heart" 
     android:layout_width="80dp" 
     android:layout_height="70dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="70dp" 
     android:background="@drawable/favorite_dialog"/> 

    <TextView 
     android:id="@+id/question_favorites" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#FFFFFF" 
     android:layout_below="@+id/heart" 
     android:layout_marginTop="10dp" 
     android:layout_marginLeft="30dp" 
     android:layout_marginRight="20dp" 
     android:layout_centerHorizontal="true" 
     android:textStyle="bold" 
     android:textSize="20sp"/> 

    <TextView 
     android:id="@+id/explanation_favorites" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#FFFFFF" 
     android:layout_below="@+id/question_favorites" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="40dp" 
     android:layout_marginRight="40dp" 
     android:textSize="18sp" 
     android:textStyle="italic" 
     android:layout_centerHorizontal="true"/> 

    <com.facebook.widget.LoginButton 
     android:id="@+id/authButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_above="@+id/button" 
     android:layout_centerHorizontal="true" 
     /> 

    <Button 
     android:id="@+id/button" 
     android:layout_height="40dp" 
     android:layout_width="200dp" 
     android:text="Cancelar" 
     android:textStyle="bold" 
     android:textSize="18sp" 
     android:layout_marginTop="10dp" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="30dp" 
     android:background="@drawable/cancel"/> 

</RelativeLayout> 

該對話框顯示在onCreate方法,它包含一個取消按鈕,其將它關閉。

我將不勝感激任何幫助。提前謝謝了。

+0

你可以發佈你的代碼嗎? – Cjames 2013-02-13 15:02:15

+0

我剛更新了,謝謝! – Sebiwi 2013-02-13 15:35:58

回答

4

我想出了這一個,它很簡單,但不那麼優雅。

我所做的就是將隱藏的主佈局內的Facebook登錄按鈕。然後,我在對話框按鈕上設置了一個偵聽器,並在其中添加了「facebookLogin.performClick();」。

將點擊重定向到facebook登錄監聽器。

謝謝反正!

0

我也打了同樣的錯誤。但最後我管理得到它的工作:

因此:MainActivity打開WelcomeFragment。 WelcomeFragment打開LoginDialog(我自己的對話框)。在LoginDialog中我有facebookLogin按鈕。 MainActivity內部我有我應該有的一切,根據Facebook的教程:UiLifecycleHelper uiHelper實例,它在onResume,onActivityResult,onPause,onDestroy和onSaveInstanceState中調用。在一個LoginDialog登錄按鈕被初始化,通過以下:

LoginButton authButton = (LoginButton) findViewById(R.id.facebookLogin); 
authButton.setReadPermissions(Arrays.asList("email")); 
authButton.setFragment(welcomeFragment); 

但是,這是行不通的,直到我說onActivityResult到WelcomeFragment,相同MainActivity:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    ((MainActivity)getActivity()).uiHelper.onActivityResult(requestCode, resultCode, data); 
} 

而且它是在https://developers.facebook.com/docs/android/login-with-facebook/v2.2描述:

「在認證流程中,默認情況下結果會返回主活動,您的活動需要通過覆蓋onActivityResult()方法來處理結果。要允許片段接收onActivityResu lt()調用而不是活動,則可以在LoginButton實例上調用setFragment()方法。「