简体   繁体   English

如何通过其他活动到达底表?

[英]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] [这是我的底页] [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: 如果您的底页是DialogFragment或BottomSheetDialogFrament, onAttach(Context context)侦听器附加到onAttach(Context context)方法中,如下所示:

define the interface in the Fragment: 在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: 最后,在显示该工作表的任何Activity中实现该接口:

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. 如果您没有使用可重复使用的底部工作表(例如,作为DialogFragment),则应在显示它的每个位置都包含此代码,但最好进行模块化。

Good Luck and Happy Coding! 祝您好运,编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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