如何为Twitter按钮添加点击监听器并实现登录请求?
Hey there! I see you’ve got the dependencies set up and initial configuration done, but the issue is you’re overriding the default click behavior of the TwitterLoginButton with your own OnClickListener—that’s why the login request isn’t firing. Let’s adjust your code to get the login flow working properly:
Step 1: Remove the Custom OnClickListener
The TwitterLoginButton is designed to handle the login trigger on its own, so your custom click listener is blocking the default action. Just delete that part entirely.
Step 2: Updated MainActivity Code
Here’s the corrected version of your code with a proper working login flow:
public class MainActivity extends AppCompatActivity { TwitterLoginButton loginButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize Twitter with your config (replace CONSUMER_KEY and CONSUMER_SECRET with your actual values!) TwitterConfig config = new TwitterConfig.Builder(this) .logger(new DefaultLogger(Log.DEBUG)) .twitterAuthConfig(new TwitterAuthConfig("YOUR_CONSUMER_KEY", "YOUR_CONSUMER_SECRET")) .debug(true) .build(); Twitter.initialize(config); // Set up the login button setupLoginButton(); } private void setupLoginButton() { loginButton = findViewById(R.id.login_button); // Set callback to handle login success/failure loginButton.setCallback(new Callback<TwitterSession>() { @Override public void success(Result<TwitterSession> result) { // Login succeeded! Retrieve user session details TwitterSession session = result.data; String userId = String.valueOf(session.getUserId()); String userName = session.getUserName(); Toast.makeText(MainActivity.this, "Logged in as " + userName, Toast.LENGTH_LONG).show(); // You can now use this session to make authenticated Twitter API calls } @Override public void failure(TwitterException exception) { // Handle login failure Toast.makeText(MainActivity.this, "Login failed: " + exception.getMessage(), Toast.LENGTH_LONG).show(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Pass the activity result to the login button to complete the flow loginButton.onActivityResult(requestCode, resultCode, data); } }
Key Reminders:
- Replace placeholders: Swap
YOUR_CONSUMER_KEYandYOUR_CONSUMER_SECRETwith the actual values from your Twitter Developer Dashboard (you get these when creating a new Twitter app). - No custom click handler needed: The
TwitterLoginButtonwill automatically launch the official Twitter login screen when tapped once you’ve set the callback. - Success callback usage: Use the
TwitterSessionobject to access user info (ID, username) and authenticate future API requests.
Quick Additional Check:
Ensure you have the internet permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
That’s it! Now when you tap the login button, it’ll trigger the standard Twitter login flow, and you’ll get the user’s session details in the success callback once they’ve logged in.
内容的提问来源于stack exchange,提问作者Andy Rubin

