Facebook登录按钮在显示新视图之前将其文本更改为“注销” [英] Facebook login button changes his text in 'log out' before new view is shown

查看:148
本文介绍了Facebook登录按钮在显示新视图之前将其文本更改为“注销”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Facebook SDK 4.0实现了通过Facebook登录我的应用程序。
当用户点击Facebook登录按钮时,他导航到家庭活动,并加载了一个新视图。问题是,一段时间后,在Facebook进程对话框消失之后,在主视图显示之前,facebook登录按钮会在注销中更改其文本,用户可以看到该文本。如何避免这种情况?



这是我片段登录中的代码:

  @Override 
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState)
{
查看视图= inflater.inflate(R.layout.frg_login,container ,假);

callbackManager = CallbackManager.Factory.create();

fbLoginButton =(LoginButton)view.findViewById(R.id.fb_login_button);
fbLoginButton.setReadPermissions(user_friends);
fbLoginButton.setReadPermissions(public_profile);
fbLoginButton.setFragment(this);
fbLoginButton.registerCallback(callbackManager,new FacebookCallback< LoginResult>(){
@Override
public void onSuccess(LoginResult loginResult){
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback(){
@Override
public void onCompleted(
JSONObject对象,
GraphResponse响应){
//应用程序代码
Log.v(LoginActivity,response.toString());
session = new SessionManager(getActivity());
try {String name = object.getString (name);
session.createLoginSession(name,true);
//Toast.m akeText(getActivity(),登录成功,Toast.LENGTH_SHORT).show();
}
catch(JSONException exe){}
}
});
Bundle parameters = new Bundle();
parameters.putString(fields,id,name);
request.setParameters(parameters);
request.executeAsync();


@Override
public void onCancel(){
Toast.makeText(getActivity(),登录取消,Toast.LENGTH_SHORT).show() ;
}

@Override
public void onError(FacebookException exception){
Toast.makeText(getActivity(),Login error,Toast.LENGTH_SHORT).show ();
}
});

返回视图;
}

在调用片段的活动中:

  if(currentAccessToken!= null){
navigatetoHomeActivity();
}
public void navigateToHomeActivity(){
Intent homeIntent = new Intent(getActivity(),MainActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}

感谢所有

解决方案

我通过在



<$旁边放置一个看不见的 TextView p $ p> < com.facebook.login.widget.LoginButton ...
>

在我的 activity_login.xml 中。这个 TextView 具有与facebook按钮相同的背景,尺寸和其他属性。在我的情况下:

 < TextView 
android:paddingTop =13sp
android:paddingBottom = 13sp
android:textSize =20sp
android:layout_width =match_parent
android:layout_height =wrap_content
android:background =@ color / com_facebook_blue
android:textColor =@ color / white
android:text =@ string / logging_in
android:textAlignment =center
android:id =@ id / textviewId
android:gravity =center
android:textStyle =bold
android:visibility =invisible
android:layout_marginBottom =35dp
android:layout_marginRight =@ dimen / activity_horizo​​ntal_margin
android:layout_marginLeft =@ dimen / activity_horizo​​ntal_margin
android:layout_alignParentBottom =true
android:layout_alignParentStart =true
android:layout_alignParentEnd =true/>

在片段的 onCreateView 添加一个 onClickListener ,您可以将 LoginButton 的可见性设置为false,并显示 TextView 克隆为true:

  fbLoginButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
fbLoginButton.setVisibility(View.INVISIBLE);
findViewById(R.id.textviewId).setVisibility(View.VISIBILE);
}
});

最后,在你的 FacebookCallback< LoginResult> 方法可以将 fbLoginButton 可见性重置为 View.VISIBLE 并隐藏 TextView 如果登录失败。



请记住在片段创建时重置可见性!


I implemented the login via facebook for my app using Facebook SDK 4.0. When the user clicks on facebook login button he navigates to home activity and a new view is loaded. The problem is that for a while, after the facebook process dialog disappears and before main view is shown, the facebook login button changes his text in 'Log out' and this is visible by the user. How can I avoid this?

This is my code in the fragment login:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.frg_login, container, false);

    callbackManager = CallbackManager.Factory.create();

    fbLoginButton = (LoginButton) view.findViewById(R.id.fb_login_button);
    fbLoginButton.setReadPermissions("user_friends");
    fbLoginButton.setReadPermissions("public_profile");
    fbLoginButton.setFragment(this);
    fbLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {
                            // Application code
                            Log.v("LoginActivity", response.toString());
                            session = new SessionManager(getActivity());
                            try{ String name = object.getString("name");
                                session.createLoginSession(name, true);
                                //Toast.makeText(getActivity(), "Login successful", Toast.LENGTH_SHORT).show();
                            }
                            catch (JSONException exe) {  }
                            }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name");
            request.setParameters(parameters);
            request.executeAsync();
        }

        @Override
        public void onCancel() {
            Toast.makeText(getActivity(), "Login canceled", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(FacebookException exception) {
            Toast.makeText(getActivity(), "Login error", Toast.LENGTH_SHORT).show();
        }
    });

    return view;
}

And in the activity that calls the fragment:

if (currentAccessToken != null) {
                    navigatetoHomeActivity();
                }
public void navigatetoHomeActivity(){
        Intent homeIntent = new Intent(getActivity(),MainActivity.class);
        homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(homeIntent);
    }

Thanks to all

解决方案

I solved this by putting an invisible TextView next to the

<com.facebook.login.widget.LoginButton ... 
> 

in my activity_login.xml. This TextView has the same background, dimensions and other properties as the facebook button. In my case:

<TextView
        android:paddingTop="13sp"
        android:paddingBottom="13sp"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/com_facebook_blue"
        android:textColor="@color/white"
        android:text="@string/logging_in"
        android:textAlignment="center"
        android:id="@+id/textviewId"
        android:gravity="center"
        android:textStyle="bold"
        android:visibility="invisible"
        android:layout_marginBottom="35dp"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"  />

In the onCreateView method of the fragment, add an onClickListener in which you set the visibility of the LoginButton to false and the visibility of your TextView clone to true with:

fbLoginButton.setOnClickListener(new OnClickListener() {
       public void onClick(View v) {
           fbLoginButton.setVisibility(View.INVISIBLE);
           findViewById(R.id.textviewId).setVisibility(View.VISIBILE);
       }
});

Finally, within your FacebookCallback<LoginResult> method you can reset the fbLoginButton visibility to View.VISIBLE and hide the TextView if the login was a failure.

Remember to reset the visibility upon fragment creation!

这篇关于Facebook登录按钮在显示新视图之前将其文本更改为“注销”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆