如何从底页导航到 activity

How to navigate from bottomsheet to activity

我在我的应用程序中实现了一个 bottomsheet。我使用 inflater 来显示 bottomsheet 对话框,这是我在 NewSignUpActivity 中显示 bottomsheet 对话框的代码。

class NewSignUpActivity : AppCompatActivity() {

    private lateinit var btnBottomSheet: RelativeLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_rider_or_bike)

        btnBottomSheet = findViewById(R.id.riderBtn)
        val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback(){
            override fun onStateChanged(bottomSheet: View, newState: Int) {}
            override fun onSlide(bottomSheet: View, slideOffset: Float) {}
        }
        val bottomSheetView = layoutInflater.inflate(R.layout.layout_bottom_sheet, null)
        val bottomSheetDialog = BottomSheetDialog(this)
        bottomSheetDialog.setContentView(bottomSheetView)
        val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView.parent as View)
        bottomSheetBehavior.setBottomSheetCallback(bottomSheetCallback)

        btnBottomSheet.setOnClickListener {
            bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
            bottomSheetDialog.show()
        }


    }
}

这是它在视图中的样子。

如您所见,底页上有一个按钮我想导航到另一个布局 activity 单击是按钮后,我该怎么做?我对使用 Kotlin android 开发还很陌生。

已编辑:为 bottomsheet 对话框添加了我的 XML 文件,请参阅 below._ 文件名是 layout_bottom_sheet.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layoutBtmSheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/rectangle"
   >


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.AppCompatImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="@dimen/_20sdp"
            android:layout_marginRight="@dimen/_15sdp"
            android:layout_marginBottom="@dimen/_30sdp"
            android:src="@drawable/close_black" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="52dp"
        android:background="@drawable/bgwhite"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/ll_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/_30sdp"
            android:layout_marginTop="@dimen/_20sdp"
            android:layout_marginRight="@dimen/_10sdp"
            android:orientation="vertical"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.AppCompatImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:src="@drawable/outline_sports_motorsports_black_48"
                tools:layout_editor_absoluteX="3dp"
                tools:layout_editor_absoluteY="3dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_10sdp"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/ll_one"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/hello_there_partner_rider"
                android:textSize="18sp"
                android:textStyle="bold" />

            <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="@dimen/_5sdp"
                android:text="@string/lorem_ipsum" />

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout">

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_100sdp"
        android:layout_marginBottom="@dimen/_10sdp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.49"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout3"
        app:layout_constraintVertical_bias="0.678">

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/noBtn"
            android:layout_width="169dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/bgbtn1"
            android:radius="@dimen/_50sdp"
            android:text="No" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/yellowBtn"
            android:layout_width="169dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:textColor="@color/white"
            android:background="@drawable/bgbtno"
            android:text="@string/yes" />
    </LinearLayout>

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"
       android:textColor="@color/reply_black_800"
        android:text="@string/are_you_using_your_own_motorcycle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        app:layout_constraintVertical_bias="0.074" />


</androidx.constraintlayout.widget.ConstraintLayout>

BottomSheetView 布局已经膨胀:

val bottomSheetView = layoutInflater.inflate(R.layout.layout_bottom_sheet, null)

并且此布局包含“是”按钮,因此您可以只使用 findViewById()。假设YES按钮的id为btn_yes:

val btnYes = bottomSheetView.findViewById<Button>(R.id.btn_yes)

btnYes.setOnClickListener {
    startActivity(myIntent) // Add your activity intent
}

更新:

因为你提供的是yellowBtn按钮的id,所以你可以使用它:

val btnYes = bottomSheetView.findViewById<Button>(R.id.yellowBtn)
btnYes.setOnClickListener {
    startActivity(myIntent) // Add your activity intent
}