繁体   English   中英

如何创建具有多个选项的弹出窗口,并且用户可以在 android(Java) 中选择多个选项

[英]how to create a pop up window with multiple options and user can choose more than one option in android(Java)

我附上了我需要实现的图片。 当我点击一个文本视图时,我需要显示一个弹出窗口,如上图所示。 用户可以选择一个或多个选项。 我创建了一个带有多个文本视图的 xml,并在对话框中扩展了该布局。 我需要的是用户如何选择多个选项并突出显示所选选项。 以及如何关闭对话框。

我的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:padding="25dp"
    android:orientation="vertical"
    android:background="@drawable/alert_bg"
    android:backgroundTint="#EDECEC"
    tools:ignore="UnusedAttribute">

        <TextView
            android:id="@+id/threeyears"
            android:layout_width="match_parent"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@android:color/transparent"
            android:layout_marginTop="10dp"
            android:fontFamily="@font/robotobold"
            android:textColor="@color/colorBlack"
            android:textSize="20sp"
            android:alpha=".4"
            android:textAllCaps="false"
            android:text="3 years and under"/>



    <TextView
        android:id="@+id/fouryears"
        android:layout_width="match_parent"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        android:fontFamily="@font/robotobold"
        android:textColor="@color/colorBlack"
        android:textSize="20sp"
        android:alpha=".4"
        android:textAllCaps="false"
        android:text=" 4 - 6 years"/>

    <TextView
        android:id="@+id/sevenyears"
        android:layout_width="match_parent"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        android:fontFamily="@font/robotobold"
        android:textColor="@color/colorBlack"
        android:textSize="20sp"
        android:alpha=".4"
        android:textAllCaps="false"
        android:text="7 - 9 years"/>
    <TextView
        android:id="@+id/tenyears"
        android:layout_width="match_parent"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        android:fontFamily="@font/robotobold"
        android:textColor="@color/colorBlack"
        android:textSize="20sp"
        android:alpha=".4"
        android:textAllCaps="false"
        android:text="10 - 12 years"/>
    <TextView
        android:id="@+id/thirteenyears"
        android:layout_width="match_parent"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:background="@android:color/transparent"
        android:fontFamily="@font/robotobold"
        android:textColor="@color/colorBlack"
        android:textSize="20sp"
        android:alpha=".4"
        android:textAllCaps="false"
        android:text="13 - 15 years"/>
    <TextView
        android:id="@+id/sixteenyears"
        android:layout_width="match_parent"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        android:fontFamily="@font/robotobold"
        android:textColor="@color/colorBlack"
        android:textSize="20sp"
        android:alpha=".4"
        android:textAllCaps="false"
        android:text="16 - 18 years"/>
    <TextView
        android:id="@+id/nochildrens"
        android:layout_width="match_parent"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        android:fontFamily="@font/robotobold"
        android:textColor="@color/colorBlack"
        android:textSize="20sp"
        android:alpha=".4"
        android:textAllCaps="false"
        android:text="No Childrens"/>
    </LinearLayout>

我的 Java 代码

parentstatustextview.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        parentaldialog = new Dialog(Objects.requireNonNull(getActivity()), R.style.CustomDialog);
        @SuppressLint("InflateParams") final View view1 = LayoutInflater.from(getActivity()).inflate(R.layout.parentalstatus, null);
        parentaldialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        parentaldialog.setCancelable(true);
        parentaldialog.setContentView(view1);
        parentaldialog.show();
        final TextView threeyears = view1.findViewById(R.id.threeyears);
        final TextView fouryears = view1.findViewById(R.id.fouryears);
        final TextView seveenyears = view1.findViewById(R.id.sevenyears);


        threeyears.setOnClickListener(this);
        fouryears.setOnClickListener(this);
        seveenyears.setOnClickListener(this);
    }
});

您应该使用accept按钮或类似按钮关闭对话框。 单击项目时,您应该将其标记为反馈用户(粗体、更改颜色或使用复选框)并将其状态存储在数组中(例如)。 当用户单击accept按钮时,从数组中取出所选项目。

暂无
暂无

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

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