Using custom login button with Twitter Fabric?

前端 未结 5 1865
名媛妹妹
名媛妹妹 2021-01-31 04:27

I have been trying to use a normal button to execute the authentication process with the twitter sdk but it does not seem to work. Anyone have tried anything similar?

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-31 04:47

    Well actually there's a way of doing this

     private TwitterAuthClient client;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    
        TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
        Fabric.with(this, new Twitter(authConfig));
        client = new TwitterAuthClient();
    
    
        Button customLoginButton = (Button) findViewById(R.id.custom_twitter_login);
        customLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                client.authorize(LoginActivity.this, new Callback() {
                    @Override
                    public void success(Result twitterSessionResult) {
                        Toast.makeText(LoginActivity.this, "success", Toast.LENGTH_SHORT).show();
                    }
    
                    @Override
                    public void failure(TwitterException e) {
                        Toast.makeText(LoginActivity.this, "failure", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        client.onActivityResult(requestCode, resultCode, data);
    }
    

    Be aware, onActivityResult part is very important, you seem to lost it.

    here's my xml:

    
    
    
    

提交回复
热议问题