android-可能在对话框中加载另一个活动

问题描述 投票:0回答:1

我是Android编程(Java)的新手。我想在进行活动X时将活动Y加载到对话框中。可以吗?如果有人可以帮助我,我将非常感谢!我很乐意获得视频,示例代码等的链接。

java android android-activity dialog
1个回答
0
投票

请确保您可以添加此内容

    Dialog dialogBuilder dialogBuilder = new Dialog(XActivity.this);

                LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View dialogView = inflater.inflate(R.layout.x_alert, null);
                ImageView close = dialogView.findViewById(R.id.close);
                Button ok = dialogView.findViewById(R.id.ok);

                ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                Intent intent = new Intent(XActivity.this,Y.class);
                startActivity(intent);
                    }
                });
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogBuilder.dismiss();
                    }
                });
                Objects.requireNonNull(dialogBuilder.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialogBuilder.setContentView(dialogView);
                dialogBuilder.setCanceledOnTouchOutside(true);
                dialogBuilder.setCancelable(true);
                dialogBuilder.show();

在此对话框的xml中(x_alert)

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">


    <androidx.cardview.widget.CardView
        android:layout_margin="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="8dp"
        app:cardElevation="10dp"
        app:elevation="15dp"
        >
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:text="@string/title"
                android:layout_gravity="center"
                android:textSize="19dp"
                android:textStyle="bold"
                android:fontFamily="@font/typo_hoop_bold"
                android:layout_marginTop="32dp"
                android:textColor="@color/colorPrimary"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:text="@string/ description "
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_margin="32dp"
                android:fontFamily="@font/oxgen"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <Button
                android:id="@+id/ok"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="16dp"
                android:layout_marginLeft="95dp"
                android:layout_marginRight="95dp"
                android:layout_marginBottom="32dp"
                android:background="@drawable/confirm_button"
                android:gravity="center"
                android:textAllCaps="false"
                android:text="@string/ok"
                android:textColor="@color/colorPrimary"
                android:textSize="16sp" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>
    </RelativeLayout>
    <ImageView
        android:id="@+id/close"
        android:src="@drawable/ic_clear_blue"
        android:layout_alignParentRight="true"
        android:layout_width="40dp"
        android:layout_height="40dp"/>
</RelativeLayout>

我希望它能与您合作。

© www.soinside.com 2019 - 2024. All rights reserved.