如何从其他Activity调用Bottom Sheet并监听其中按钮点击?
Hey there! Let's figure out why your "Pay Now" button isn't responding. Since you're using an Activity as your bottom sheet, there are a few key things to check and fix:
1. Fix the Button Initialization & Click Listener
First, make sure you're properly finding the button and attaching the click listener in your SheetActivity's onCreate method. Here's the complete implementation based on your partial code:
public class SheetActivity extends AppCompatActivity { Button sheet_button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sheet); // Replace with your actual button ID from activity_sheet.xml sheet_button = findViewById(R.id.btn_pay_now); // Set up the click listener for "Pay Now" sheet_button.setOnClickListener(v -> { // Add your payment logic here (e.g., validate inputs, trigger payment flow) Toast.makeText(this, "Pay Now action triggered!", Toast.LENGTH_SHORT).show(); finish(); // Close the bottom sheet activity after handling the action }); } }
2. Verify Your Activity's Theme & Window Properties
A common pitfall with Activity-based bottom sheets is incorrect window settings that block touch events. Update your AndroidManifest.xml to configure the SheetActivity correctly:
<activity android:name=".SheetActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar" android:windowIsTranslucent="true" android:windowSoftInputMode="adjustResize" android:windowBackground="@android:color/transparent" android:launchMode="singleInstance"/>
These settings ensure:
- No default action bar interferes with your sheet layout
- The window is translucent (so the underlying activity remains visible)
- Touch events are properly routed to your sheet's views
3. Check Your Layout for Potential Blockages
Double-check your activity_sheet.xml to rule out these issues:
- Confirm the button's ID matches exactly what you use in
findViewById - Make sure no other view (like a transparent overlay or oversized parent layout) is covering the button
- Ensure the button's
android:clickableattribute is set totrue(this is the default, but it's worth verifying if you modified the layout)
4. Bonus: Add Bottom Sheet Slide Animations (For Better UX)
While this doesn't fix the click issue, adding slide animations will make your Activity feel like a proper bottom sheet. Create two animation files:
res/anim/slide_up.xml
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromYDelta="100%p" android:toYDelta="0%p" android:duration="300"/>
res/anim/slide_down.xml
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromYDelta="0%p" android:toYDelta="100%p" android:duration="300"/>
Then add an animation style to styles.xml:
<style name="BottomSheetAnimation"> <item name="android:windowEnterAnimation">@anim/slide_up</item> <item name="android:windowExitAnimation">@anim/slide_down</item> </style>
Finally, update the SheetActivity entry in AndroidManifest.xml to use this style:
<activity android:name=".SheetActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar" android:windowIsTranslucent="true" android:windowSoftInputMode="adjustResize" android:windowBackground="@android:color/transparent" android:launchMode="singleInstance" android:windowAnimationStyle="@style/BottomSheetAnimation"/>
Most likely, your issue was just missing the proper button initialization or click listener setup. Give these steps a try, and your "Pay Now" button should start working!
内容的提问来源于stack exchange,提问作者iosdev

