简体   繁体   中英

How to Reach Bottom Sheet from another activity?

I tried to use a bottom sheet which use a layout of an activity, not dialog.

[Here is my bottom sheet][1]

But I can't access "Pay Now" button. I tried to use click listener in the same activity, but nothing happens. How can I listen this button and where?

Activity of Bottom Sheet

public class SheetActivity extends AppCompatActivity {

Button sheet_button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sheet);

    sheet_button = (Button)findViewById(R.id.sheet_button);
    //

    sheet_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Done!", Toast.LENGTH_SHORT).show();
            sheet_button.setText("deneme");
        }
    });
}}

This is generally pretty simple.

If your bottom sheet is a DialogFragment or BottomSheetDialogFrament attach a listener into the onAttach(Context context) method like so:

define the interface in the Fragment:

interface CheckoutButtonListener {
    void onClick(/*provide whatever arguments you need to back to parent*/);
}

usage:

// Define the member variable
private CheckoutButtonListener mCheckoutListener;

@Override
public void onAttach(Context context){
    super.onAttach(context);
    try{
        mCheckoutListener = (CheckoutButtonListener) context;
    }catch(ClassCastException){
      // Handle the error silently or rethrow so usage is expected
    }
}

Then when you attach the listener to the button:

mButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        mCheckoutListener.onClick(); // call the interface method
    }
});

Finally, implement the interface in whatever Activity is showing the sheet:

class MyActivity extends AppCompatActivity implements CheckoutButtonListener {

     //.. other code

     // interface method
     @Override
     public void onClick(){
        // do whatever you need to do
     }
}

If you are not using a re-usable bottom sheet (ie as a DialogFragment) then you should include this code in each place you display it, however better to modularize.

Good Luck and Happy Coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM