Android Bottom Sheet Dialog 布局有按钮,点击导航到 activity

Android BottomSheetDialog layout have button, onclick navigat to activity

我的 activity 调用了 BottomSheetDialog 布局 xml 文件。 BottomSheetDialog 有 2 个按钮。单击 BottomSheetDialog 内的按钮,它应该将我带到另一个 Activity.

main_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ezeelearn.colorstonz.ezeelearn.PhysicsTestSelectorActivity"
    tools:showIn="@layout/app_bar_physics_lesson">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:id="@+id/lesson_list">



        </TableLayout>


    </ScrollView>

    <include layout="@layout/bottom_sheet_layout" />


</LinearLayout>

bottom_sheet_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/RelativeLayoutSheet"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/bottom_sheet_behavior"
    android:background="#ffffff"
    app:behavior_hideable="true"
    android:padding="20dp"
    >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:paddingRight="50dp"
                android:paddingLeft="50dp"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_weight="1"
                    android:background="#fff"
                    android:drawableTop="@drawable/google_cardboard"
                    android:drawablePadding="6dp"
                    android:gravity="left|center"
                    android:height="60dp"
                    android:padding="6dp"
                    android:text="Video"
                    android:id="@+id/bottom_sheet_video_btn"
                    android:textAlignment="center"
                    android:fontFamily="sans-serif-light"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:textColor="#666" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:paddingRight="50dp"
                android:paddingLeft="50dp"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_weight="1"
                    android:background="#fff"
                    android:drawableTop="@drawable/bulletin_board"
                    android:drawablePadding="6dp"
                    android:gravity="left|center"
                    android:height="60dp"
                    android:padding="6dp"
                    android:text="Lesson"
                    android:id="@+id/bottom_sheet_lesson_btn"
                    android:textAlignment="center"
                    android:fontFamily="sans-serif-light"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:textColor="#666" />

            </LinearLayout>

        </TableRow>

    </TableLayout>



</LinearLayout>

MainActivity.java

public class MainActivity extends Activity{

    BottomSheetDialog bottomSheetDialog;
    BottomSheetBehavior bottomSheetBehavior ;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        List<String> lessonNames = new ArrayList<String>();
        lessonNames.add("Photosynthesis in Higher Plants");
        lessonNames.add("The Living World");
        lessonNames.add("Biological Classification");
        lessonNames.add("Plant Kingdom");
        lessonNames.add("Animal Kingdom");
        lessonNames.add("Morphology of Flowering Plants");
        lessonNames.add("Anatomy of Flowering Plants");
        lessonNames.add("Structural Organisation in Animals");
        lessonNames.add("Cell-The Unit of Life");
        lessonNames.add("Biomolecules");
        lessonNames.add("Transport in Plants");
        lessonNames.add("Mineral Nutrition");
        lessonNames.add("Flowering Plants");

        TableLayout lessonList = (TableLayout) findViewById(R.id.lesson_list);
        List<TableLayout> lessonTableList = new ArrayList<TableLayout>();

        for(int i = 0; i < lessonNames.size(); i++) {

            TableRow tableRow = new TableRow(this);
            TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
            tableRow.setLayoutParams(tableLayoutParams);
            tableRow.setPadding(30, 25, 30, 25);
            tableRow.setBackgroundDrawable(getResources().getDrawable(R.drawable.textlines));


            TextView textView = new TextView(this);
            textView.setText(String.format("%02d", (i+1))+".  "+lessonNames.get(i));
            /*textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_dots_vertical, 0);*/
            textView.setTextSize(17);

            tableRow.addView(textView);


            /*TextView textView = new TextView(this);
            textView.setText(String.format("%02d", (i+1))+".  "+lessonNames.get(i));
            textView.setPadding(20, 25, 20, 25);
            textView.setTextSize(17);
            textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_dots_vertical, 0);
            textView.setLayoutParams(new TableLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

            textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.textlines));

            lessonList.addView(textView);*/
            lessonList.addView(tableRow);
            lessonTableList.add(lessonList);
        }

        for(final TableLayout tableLayout: lessonTableList) {
            tableLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bottomSheetDialog = new BottomSheetDialog(PhysicsLessonActivity.this);

                    View view = getLayoutInflater().inflate(R.layout.bottom_sheet_layout, null);
                    bottomSheetDialog.setContentView(view);
                    bottomSheetDialog.show();


                    bottomSheetDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                        @Override
                        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                            Log.d("BottomSheetVideoBtn", "called");
                            return false;
                        }
                    });


                }
            });
        }

        Button bottomSheetVideoBtn = (Button) findViewById(R.id.bottom_sheet_video_btn);
        Button bottomSheetLessonBtn = (Button) findViewById(R.id.bottom_sheet_lesson_btn);

        bottomSheetVideoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), PhysicsActivity.class);
                Log.d("BottomSheetVideoBtn", "called");
                startActivity(intent);
            }
        });

        bottomSheetLessonBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getBaseContext(), "Lesson Button Clicked", Toast.LENGTH_LONG).show();
            }
        });

        ImageButton backToPHome = (ImageButton) findViewById(R.id.back_to_home);
        backToPHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), PhysicsActivity.class);
                startActivity(intent);
            }
        });

    }

}

使用这个

  public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false);
        Button btn1 = (Button)v.findViewById(R.id.btn1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),YourActivity.class));
            }
        });
        return v;
    }
 }

然后通过

在您的 activity 中调用这个 bottomSheet
new CustomBottomSheetDialogFragment().show(getSupportFragmentManager(), "Dialog");
Button bottomSheetVideoBtn = (Button) view.findViewById(R.id.bottom_sheet_video_btn);

Button bottomSheetLessonBtn = (Button) view.findViewById(R.id.bottom_sheet_lesson_btn);

而不是创建新的 class,这个怎么样

final BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);
            dialog.setContentView(R.layout.your_bottomsheet_layout);
            dialog.setCanceledOnTouchOutside(false);

            ImageButton btnClose = (ImageButton) dialog.findViewById(R.id.button_close);
            btnClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.show();