问题
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?
- I have correctly setup the API keys, etc..
- The login process execute correctly but the callback part seems not to be called.
- None of my logs are executed (Neither the success or failure part)
The code
buttonTwitterLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Twitter.logIn(LoginActivity1.this, new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> twitterSessionResult) {
Log.i(TAG, "success");
Log.i(TAG, twitterSessionResult.toString());
}
@Override
public void failure(TwitterException e) {
Log.e(TAG, "failed");
}
});
}
});
回答1:
Luis from Developer Relations team at Twitter. Fabric will support theming on the future, meanwhile you can customize the button by creating a custom view that inherits from TwitterLoginButton
.
Cannonball sample app implements a custom button:
public class CannonballTwitterLoginButton extends TwitterLoginButton {
public CannonballTwitterLoginButton(Context context) {
super(context);
init();
}
public CannonballTwitterLoginButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CannonballTwitterLoginButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
if (isInEditMode()){
return;
}
setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable
.ic_signin_twitter), null, null, null);
setBackgroundResource(R.drawable.sign_up_button);
setTextSize(20);
setPadding(30, 0, 10, 0);
setTextColor(getResources().getColor(R.color.tw__blue_default));
setTypeface(App.getInstance().getTypeface());
}
}
https://github.com/twitterdev/cannonball-android/blob/master/app/src/main/java/io/fabric/samples/cannonball/view/CannonballTwitterLoginButton.java
Layout file:
<io.fabric.samples.cannonball.view.CannonballTwitterLoginButton
android:id="@+id/twitter_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/sign_up_button"
android:layout_marginTop="@dimen/login_button_margin_top"
android:layout_marginBottom="@dimen/login_button_margin_bottom"
android:layout_marginLeft="@dimen/login_button_margin_start"
android:layout_marginStart="@dimen/login_button_margin_start"
android:layout_marginRight="@dimen/login_button_margin_end"
android:layout_marginEnd="@dimen/login_button_margin_end"
android:text="@string/sign_in_with_twitter"
android:textColor="@color/grayish_blue"
android:textSize="@dimen/login_button_text_size"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
https://github.com/twitterdev/cannonball-android/blob/master/app/src/main/res/layout/activity_login.xml
Callback setup:
private void setUpTwitterButton() {
twitterButton = (TwitterLoginButton) findViewById(R.id.twitter_button);
twitterButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
SessionRecorder.recordSessionActive("Login: twitter account active", result.data);
startThemeChooser();
}
@Override
public void failure(TwitterException exception) {
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.toast_twitter_signin_fail),
Toast.LENGTH_SHORT).show();
Crashlytics.logException(exception);
}
});
}
https://github.com/twitterdev/cannonball-android/blob/master/app/src/main/java/io/fabric/samples/cannonball/activity/LoginActivity.java
I strongly recommend you to clone the code and take a look on it.
回答2:
You can achieve this by using TwitterAuthClient
. i.e,
First of all create normal button like,
<Button
android:id:"@+id/twitter_custom_button"
... />
Now, in you java class file use TwitterAuthClient
instead of TwitterLoginButton
. then set your CallBack
inside Button
's onClick
TwitterAuthClient mTwitterAuthClient= new TwitterAuthClient();
Button twitter_custom_button = (Button) findViewById(R.id.twitter_custom_button);
twitter_custom_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTwitterAuthClient.authorize(this, new com.twitter.sdk.android.core.Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> twitterSessionResult) {
// Success
}
@Override
public void failure(TwitterException e) {
e.printStackTrace();
}
});
}
});
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
mTwitterAuthClient.onActivityResult(requestCode, responseCode, intent);
}
回答3:
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<TwitterSession>() {
@Override
public void success(Result<TwitterSession> 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:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/custom_twitter_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding_medium"
android:text="@string/twitter_login"/>
回答4:
Since Fabric doesn't allow theming yet, we should assume that we'll have to write potentially throw away code to get this to work. As such, I prefer less code to more code when I know I won't be keeping it.
Luis's solution will work fine, but my UI was done, with a button already, and I just wanted that button to work. Here's how I accomplished this.
Drop in the TwitterLoginButton, as requested by the Fabric wizard, but set it to visibility: gone
<com.twitter.sdk.android.core.identity.TwitterLoginButton
android:id="@+id/twitter_login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
Pull the reference of the loginButton in your Activity (also part of Fabric setup - nothing new here):
loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
Finally, for my existing button, I added a click listener, which simply delegated the click event over to the twitter login button.
myLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginButton.performClick();
}
});
Worked like a charm.
回答5:
You can also avoid creating a CustomTwitterLoginButton class by doing the default implementation of the twitter login button and then doing this:
twitterLoginButton.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
twitterLoginButton.setBackgroundResource(R.drawable.btn_twitter);
twitterLoginButton.setCompoundDrawablePadding(0);
twitterLoginButton.setPadding(0, 0, 0, 0);
twitterLoginButton.setText("Login with Twitter");
twitterLoginButton.setTextSize(18);
来源:https://stackoverflow.com/questions/27267809/using-custom-login-button-with-twitter-fabric