Android 如何在ImageView';s单击,而不是TwitterButton';点击
下面是来自的代码。我如何将Android 如何在ImageView';s单击,而不是TwitterButton';点击,android,twitter-fabric,Android,Twitter Fabric,下面是来自的代码。我如何将setCallback()分配到ImageView,因为我需要在单击ImageView后进行身份验证(而不是twitter按钮): 有什么想法吗?这是我的解决方案 您需要编写TwitterLoginImageView.class: public class TwitterLoginImageView extends ImageView { final static String TAG = TwitterCore.TAG; static final St
setCallback()
分配到ImageView
,因为我需要在单击ImageView
后进行身份验证(而不是twitter按钮
):
有什么想法吗?这是我的解决方案
您需要编写TwitterLoginImageView.class
:
public class TwitterLoginImageView extends ImageView {
final static String TAG = TwitterCore.TAG;
static final String ERROR_MSG_NO_ACTIVITY = "TwitterLoginImageView requires an activity."
+ " Override getActivity to provide the activity for this button.";
final WeakReference<Activity> activityRef;
volatile TwitterAuthClient authClient;
OnClickListener onClickListener;
Callback<TwitterSession> callback;
public TwitterLoginImageView(Context context) {
this(context, null);
}
public TwitterLoginImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0); // 0 = no style will be applied
}
public TwitterLoginImageView(Context context, AttributeSet attrs, int defStyle) {
this(context, attrs, defStyle, null);
}
TwitterLoginImageView(Context context, AttributeSet attrs, int defStyle,
TwitterAuthClient authClient) {
super(context, attrs, defStyle);
this.activityRef = new WeakReference<>(getActivity());
this.authClient = authClient;
// setupImageView();
super.setOnClickListener(new LoginClickListener());
checkTwitterCoreAndEnable();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setupImageView() {
// final Resources res = getResources();
// super.setCompoundDrawablesWithIntrinsicBounds(
// res.getDrawable(com.twitter.sdk.android.core.R.drawable.tw__ic_logo_default), null, null, null);
// super.setCompoundDrawablePadding(
// res.getDimensionPixelSize(com.twitter.sdk.android.core.R.dimen.tw__login_btn_drawable_padding));
// super.setText(com.twitter.sdk.android.core.R.string.tw__login_btn_txt);
// super.setTextColor(res.getColor(com.twitter.sdk.android.core.R.color.tw__solid_white));
// super.setTextSize(TypedValue.COMPLEX_UNIT_PX,
// res.getDimensionPixelSize(com.twitter.sdk.android.core.R.dimen.tw__login_btn_text_size));
// super.setTypeface(Typeface.DEFAULT_BOLD);
// super.setPadding(res.getDimensionPixelSize(com.twitter.sdk.android.core.R.dimen.tw__login_btn_left_padding), 0,
// res.getDimensionPixelSize(com.twitter.sdk.android.core.R.dimen.tw__login_btn_right_padding), 0);
// super.setBackgroundResource(com.twitter.sdk.android.core.R.drawable.tw__login_btn);
super.setOnClickListener(new LoginClickListener());
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// super.setAllCaps(false);
// }
}
/**
* Sets the {@link com.twitter.sdk.android.core.Callback} to invoke when login completes.
*
* @param callback The callback interface to invoke when login completes.
* @throws java.lang.IllegalArgumentException if callback is null.
*/
public void setCallback(Callback<TwitterSession> callback) {
if (callback == null) {
throw new IllegalArgumentException("Callback cannot be null");
}
this.callback = callback;
}
/**
* @return the current {@link com.twitter.sdk.android.core.Callback}
*/
public Callback<TwitterSession> getCallback() {
return callback;
}
/**
* Call this method when {@link android.app.Activity#onActivityResult(int, int, Intent)}
* is called to complete the authorization flow.
*
* @param requestCode the request code used for SSO
* @param resultCode the result code returned by the SSO activity
* @param data the result data returned by the SSO activity
*/
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == getTwitterAuthClient().getRequestCode()) {
getTwitterAuthClient().onActivityResult(requestCode, resultCode, data);
}
}
/**
* Gets the activity. Override this method if this button was created with a non-Activity
* context.
*/
protected Activity getActivity() {
if (getContext() instanceof Activity) {
return (Activity) getContext();
} else if (isInEditMode()) {
return null;
} else {
throw new IllegalStateException(ERROR_MSG_NO_ACTIVITY);
}
}
@Override
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
private class LoginClickListener implements OnClickListener {
@Override
public void onClick(View view) {
checkCallback(callback);
checkActivity(activityRef.get());
getTwitterAuthClient().authorize(activityRef.get(), callback);
if (onClickListener != null) {
onClickListener.onClick(view);
}
}
private void checkCallback(Callback callback) {
if (callback == null) {
CommonUtils.logOrThrowIllegalStateException(TwitterCore.TAG,
"Callback must not be null, did you call setCallback?");
}
}
private void checkActivity(Activity activity) {
if (activity == null || activity.isFinishing()) {
CommonUtils.logOrThrowIllegalStateException(TwitterCore.TAG,
ERROR_MSG_NO_ACTIVITY);
}
}
}
TwitterAuthClient getTwitterAuthClient() {
if (authClient == null) {
synchronized (TwitterLoginImageView.class) {
if (authClient == null) {
authClient = new TwitterAuthClient();
}
}
}
return authClient;
}
private void checkTwitterCoreAndEnable() {
//Default (Enabled) in edit mode
if (isInEditMode()) return;
try {
TwitterCore.getInstance();
} catch (IllegalStateException ex) {
//Disable if TwitterCore hasn't started
Fabric.getLogger().e(TAG, ex.getMessage());
setEnabled(false);
}
}
}
之后,您需要在活动
/片段
文件中初始化此TwitterLoginImageView
:
使用ButterKnife
:
@BindView(R.id.ttImageView)
TwitterLoginImageView ttImageView;
使用标准方法:
TwitterLoginImageView ttImageView;
在onCreate()
方法中:
ttImageView = (TwitterLoginImageView) findViewById(R.id.ttImageView);
最后,您需要继续使用此网站:
这对我有帮助 这是我的解决方案
您需要编写TwitterLoginImageView.class
:
public class TwitterLoginImageView extends ImageView {
final static String TAG = TwitterCore.TAG;
static final String ERROR_MSG_NO_ACTIVITY = "TwitterLoginImageView requires an activity."
+ " Override getActivity to provide the activity for this button.";
final WeakReference<Activity> activityRef;
volatile TwitterAuthClient authClient;
OnClickListener onClickListener;
Callback<TwitterSession> callback;
public TwitterLoginImageView(Context context) {
this(context, null);
}
public TwitterLoginImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0); // 0 = no style will be applied
}
public TwitterLoginImageView(Context context, AttributeSet attrs, int defStyle) {
this(context, attrs, defStyle, null);
}
TwitterLoginImageView(Context context, AttributeSet attrs, int defStyle,
TwitterAuthClient authClient) {
super(context, attrs, defStyle);
this.activityRef = new WeakReference<>(getActivity());
this.authClient = authClient;
// setupImageView();
super.setOnClickListener(new LoginClickListener());
checkTwitterCoreAndEnable();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setupImageView() {
// final Resources res = getResources();
// super.setCompoundDrawablesWithIntrinsicBounds(
// res.getDrawable(com.twitter.sdk.android.core.R.drawable.tw__ic_logo_default), null, null, null);
// super.setCompoundDrawablePadding(
// res.getDimensionPixelSize(com.twitter.sdk.android.core.R.dimen.tw__login_btn_drawable_padding));
// super.setText(com.twitter.sdk.android.core.R.string.tw__login_btn_txt);
// super.setTextColor(res.getColor(com.twitter.sdk.android.core.R.color.tw__solid_white));
// super.setTextSize(TypedValue.COMPLEX_UNIT_PX,
// res.getDimensionPixelSize(com.twitter.sdk.android.core.R.dimen.tw__login_btn_text_size));
// super.setTypeface(Typeface.DEFAULT_BOLD);
// super.setPadding(res.getDimensionPixelSize(com.twitter.sdk.android.core.R.dimen.tw__login_btn_left_padding), 0,
// res.getDimensionPixelSize(com.twitter.sdk.android.core.R.dimen.tw__login_btn_right_padding), 0);
// super.setBackgroundResource(com.twitter.sdk.android.core.R.drawable.tw__login_btn);
super.setOnClickListener(new LoginClickListener());
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// super.setAllCaps(false);
// }
}
/**
* Sets the {@link com.twitter.sdk.android.core.Callback} to invoke when login completes.
*
* @param callback The callback interface to invoke when login completes.
* @throws java.lang.IllegalArgumentException if callback is null.
*/
public void setCallback(Callback<TwitterSession> callback) {
if (callback == null) {
throw new IllegalArgumentException("Callback cannot be null");
}
this.callback = callback;
}
/**
* @return the current {@link com.twitter.sdk.android.core.Callback}
*/
public Callback<TwitterSession> getCallback() {
return callback;
}
/**
* Call this method when {@link android.app.Activity#onActivityResult(int, int, Intent)}
* is called to complete the authorization flow.
*
* @param requestCode the request code used for SSO
* @param resultCode the result code returned by the SSO activity
* @param data the result data returned by the SSO activity
*/
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == getTwitterAuthClient().getRequestCode()) {
getTwitterAuthClient().onActivityResult(requestCode, resultCode, data);
}
}
/**
* Gets the activity. Override this method if this button was created with a non-Activity
* context.
*/
protected Activity getActivity() {
if (getContext() instanceof Activity) {
return (Activity) getContext();
} else if (isInEditMode()) {
return null;
} else {
throw new IllegalStateException(ERROR_MSG_NO_ACTIVITY);
}
}
@Override
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
private class LoginClickListener implements OnClickListener {
@Override
public void onClick(View view) {
checkCallback(callback);
checkActivity(activityRef.get());
getTwitterAuthClient().authorize(activityRef.get(), callback);
if (onClickListener != null) {
onClickListener.onClick(view);
}
}
private void checkCallback(Callback callback) {
if (callback == null) {
CommonUtils.logOrThrowIllegalStateException(TwitterCore.TAG,
"Callback must not be null, did you call setCallback?");
}
}
private void checkActivity(Activity activity) {
if (activity == null || activity.isFinishing()) {
CommonUtils.logOrThrowIllegalStateException(TwitterCore.TAG,
ERROR_MSG_NO_ACTIVITY);
}
}
}
TwitterAuthClient getTwitterAuthClient() {
if (authClient == null) {
synchronized (TwitterLoginImageView.class) {
if (authClient == null) {
authClient = new TwitterAuthClient();
}
}
}
return authClient;
}
private void checkTwitterCoreAndEnable() {
//Default (Enabled) in edit mode
if (isInEditMode()) return;
try {
TwitterCore.getInstance();
} catch (IllegalStateException ex) {
//Disable if TwitterCore hasn't started
Fabric.getLogger().e(TAG, ex.getMessage());
setEnabled(false);
}
}
}
之后,您需要在活动
/片段
文件中初始化此TwitterLoginImageView
:
使用ButterKnife
:
@BindView(R.id.ttImageView)
TwitterLoginImageView ttImageView;
使用标准方法:
TwitterLoginImageView ttImageView;
在onCreate()
方法中:
ttImageView = (TwitterLoginImageView) findViewById(R.id.ttImageView);
最后,您需要继续此站点:
这对我有帮助 你不需要为此编写额外的类。您可以使用任何自定义布局登录twitter。我已经回答了一个与你类似的问题
请参考链接。您不需要为此编写额外的类。您可以使用任何自定义布局登录twitter。我已经回答了一个与你类似的问题
请参阅链接。为什么不隐藏
twitter按钮
,并将您的ImageView
的点击委托给它?@Sufian我试图放置twitter按钮
,但看起来它被禁用了。。。为什么呢?(我在手机上安装了Twitter
)。好的。这是因为我得到了TwitterAuthConfig authConfig=newtwitterAuthConfig(getString(R.string.twitter\u consumer\u key),getString(R.string.twitter\u consumer\u secret))代码>和Fabric.with(这是新的Twitter(authConfig))
在setContentView(layout.xml)
之后…你现在解决了吗?是的,它现在可以工作了。你为什么不隐藏TwitterButton
并将你的ImageView
的点击委托给它呢?@Sufian我试图把TwitterButton
,但看起来它被禁用了。。。为什么呢?(我在手机上安装了Twitter
)。好的。这是因为我得到了TwitterAuthConfig authConfig=newtwitterAuthConfig(getString(R.string.twitter\u consumer\u key),getString(R.string.twitter\u consumer\u secret))代码>和Fabric.with(这是新的Twitter(authConfig))
在setContentView(layout.xml)
之后……您现在解决了吗?是的,它现在可以工作了。