简体   繁体   English

Android-隐藏Facebook登录按钮

[英]Android - hide Facebook login button

I followed facebook documentation to create a simple app with a login button. 我遵循了Facebook文档 ,使用登录按钮创建了一个简单的应用程序。

Now, I wish to hide the login button after the user has already logged in. 现在,我希望在用户已经登录后隐藏登录按钮。

I read here on stackoverflow that I should save the access token string and it's expiry date, and check if it's 'expired' (ignoring the case it can be expired at any time by facebook - just theoreticaly) or if it wasn't inserted yet (points that user hasn't logged in), and then show/hide the button respectively. 我在stackoverflow上阅读过,我应该保存访问令牌字符串及其有效期限,并检查其是否已“过期”(忽略Facebook可以在任何时候使之过期的情况-只是理论上而言)或是否尚未插入(指示用户尚未登录),然后分别显示/隐藏按钮。

I tried adjusting the following method: 我尝试调整以下方法:

@Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, 
            Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.activity_main, container, false);
    LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
    authButton.setFragment(this);
    authButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
    if(!hasToShowLogin()){
        authButton.setVisibility(View.GONE);
    }


    return view;
}

the code in the method 'hasToShowButton' is not relevant because it works, it's just for the demo. 方法“ hasToShowButton”中的代码不相关,因为它可以工作,仅用于演示。

The code above should hide button when the view is first created, and this one should hide the button after the login accomplished, and located in OnActivityResult: 上面的代码应在首次创建视图时隐藏按钮,而该代码应在登录完成后位于OnActivityResult中隐藏按钮:

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

    View view = getView();
    LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
    authButton.setVisibility(View.GONE);
}

What happens is that the button keep showing no matter what, and is working as usual. 发生的事情是该按钮无论如何一直显示,并且照常工作。

So to conclude, what I'm trying to do is: 总结一下,我想做的是:

  1. Hide login button after successful login 成功登录后隐藏登录按钮
  2. Hide login button in the following times the user enters the app 在用户进入应用程序的以下次数中,隐藏登录按钮

(In other words: never show login button when user is logged in) (换句话说:用户登录时从不显示登录按钮)

Been breaking my head over this for HOURS. 在HOURS上为此感到震惊。 thanks for the helpers! 谢谢帮手!

PS Just noticed that although setVisibility doesn't hide the button, the button doesn't work after the login. PS Just注意到,尽管setVisibility不会隐藏按钮,但登录后该按钮不起作用。 So it's halfway there, I just need to hide it from screen too... 所以它已经到一半了,我也只需要把它从屏幕上隐藏起来...

This worked for me. 这对我有用。

boolean loggedIn = AccessToken.getCurrentAccessToken() != null;
    if (loggedIn){
        loginButton.setVisibility(View.GONE);
    }
    else{
        loginButton.setVisibility(View.VISIBLE);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM