首页 > 解决方案 > 自定义 AlertDialog.Builder 正面和负面按钮未显示

问题描述

您好,一旦我开始显示我的 RecyclerView,我就遵循了 Stack Overflow 和 YouTube 中的每个教程,然后我需要毫无问题地完成其他事情。无论如何,我唯一的问题是正面按钮和负面按钮消失了。我认为问题可能是我在 XML 中所做的事情与视图中的按钮重叠,但我在 RecyclerView 中只有 3 个项目,所以这是我的大疑问。我在 onLongClick 中创建了对话框,然后我添加了一个 RecyclerView 方法来获取行中存在的位置和数据,这就是为什么我不能在另一个地方调用 alertDialog.setPositiveButton() 的原因。这是一个相当大的挑战,如果能在这方面获得一些帮助,我们将不胜感激。

这是我的 XML

<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <TextView
        android:id="@+id/text_adm_asigna_asesor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:fontFamily="@font/poppins_bold"
        android:text="Asigna un asesor"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="30dp"
        android:visibility="visible"
        android:layout_marginRight="30dp" />

        <LinearLayout
            android:id="@+id/linear_dialog_testDrive"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:visibility="gone"
           >

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/circle_imagen_seleccion_alert"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:visibility="gone"
                android:src="@drawable/lifeon_logo"
                android:layout_marginLeft="40dp">

            </de.hdodenhof.circleimageview.CircleImageView>

            <TextView
                android:id="@+id/alert_cambia_test_drive_nombre"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/poppins_bold"
                android:textColor="@android:color/black"
                android:text="@string/nombre"
                android:visibility="gone"
                android:layout_marginLeft="30dp">

            </TextView>

        </LinearLayout>

        <Button
            android:id="@+id/cambiar_seleccion_alert_test_drive"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:background="@drawable/button_shape"
            android:text="@string/cambiar_seleccion"
            android:layout_gravity="center_horizontal"
            android:visibility="gone"
            >

        </Button>

    </LinearLayout>
    </LinearLayout>

</androidx.core.widget.NestedScrollView>

这是java代码

      recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {

        }

        @Override
        public void onLongClick(View view, int position) {
            asesores.clear();

            ParseObject itemTestDrive = listadoTestDrive.get(position);

            testDriveObjectId = itemTestDrive.getObjectId();

            LayoutInflater inflater = getLayoutInflater();
            View convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.activity_agreements, null);
            alertDialog.setView(convertView);


            RecyclerView list = convertView.findViewById(R.id.list);
            TextView txt = convertView.findViewById(R.id.text_adm_asigna_asesor);
            CircleImageView ima=convertView.findViewById(R.id.circle_imagen_seleccion_alert);
            TextView txtNombreSeleccion=convertView.findViewById(R.id.alert_cambia_test_drive_nombre);
            Button botonCambiarSeleccion=convertView.findViewById(R.id.cambiar_seleccion_alert_test_drive);
            LinearLayout linearLayout=convertView.findViewById(R.id.linear_dialog_testDrive);


            botonCambiarSeleccion.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    list.setVisibility(View.VISIBLE);
                    ima.setVisibility(View.GONE);
                    txtNombreSeleccion.setVisibility(View.GONE);
                    botonCambiarSeleccion.setVisibility(View.GONE);
                    linearLayout.setVisibility(View.GONE);
                }
            });

            ParseQuery<ParseObject> query1 = ParseQuery.getQuery("CitaTestDrive");
            query1.include("ComercialAsignado");
            query1.getInBackground(testDriveObjectId, new GetCallback<ParseObject>() {
                @Override
                public void done(ParseObject object, ParseException e) {
                    if (object.getParseObject("ComercialAsignado") != null) {
                        txt.setText(getActivity().getResources().getString(R.string.Advertencia_asignacion_a_alguien));

                    }


                }
            });



            ParseUser user = ParseUser.getCurrentUser();

            ParseObject sucursal = user.getParseObject("PerteneceSucursal");
            String sucurObjectId = sucursal.getObjectId();

            ParseQuery<ParseUser> query = ParseUser.getQuery();
            ParseObject suc = ParseObject.createWithoutData("Sucursal", sucurObjectId);
            query.whereEqualTo("PerteneceSucursal", suc);
            query.include("PerteneceSucursal");
            query.whereEqualTo("Rol","Asesor");
            query.findInBackground(new FindCallback<ParseUser>() {
                @Override
                public void done(List<ParseUser> objects, ParseException e) {
                    if (e == null) {
                        for (ParseUser obj : objects) {
                            asesores.add(obj);
                        }
                        list.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                        list.setHasFixedSize(true);
                        ListAsesorAdminAsignaTestDrive adapter = new ListAsesorAdminAsignaTestDrive(getApplicationContext(), asesores);
                        list.setAdapter(adapter);
                    }
                }
            });
            list.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), list, new RecyclerTouchListener.ClickListener() {
                @Override
                public void onClick(View view, int position) {
                    ParseUser item = asesores.get(position);

                    list.setVisibility(view.GONE);
                    ima.setVisibility(view.VISIBLE);
                    txtNombreSeleccion.setVisibility(view.VISIBLE);
                    botonCambiarSeleccion.setVisibility(View.VISIBLE);
                    linearLayout.setVisibility(View.VISIBLE);


                    ParseFile parseFile = item.getParseFile("Avatar");
                    if (parseFile != null) {
                        parseFile.getDataInBackground(new GetDataCallback() {
                            @Override
                            public void done(byte[] data, ParseException e) {
                                if (e == null) {
                                    Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                                    if (bmp != null) {
                                        ima.setImageBitmap(bmp);
                                    }
                                }
                            }
                        });
                    }

                    String asesorObjectId = item.getObjectId();
                    String asesorNombre = item.getString("Nombre");
                    txtNombreSeleccion.setText(asesorNombre);


                    alertDialog.setPositiveButton(getApplicationContext().getResources().getString(R.string.guardar), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ParseQuery<ParseObject> query = ParseQuery.getQuery("CitaTestDrive");

                            query.getInBackground(testDriveObjectId, new GetCallback<ParseObject>() {
                                @Override
                                public void done(ParseObject object, ParseException e) {

                                    object.put("ComercialAsignado", ParseObject.createWithoutData("_User", asesorObjectId));
                                    object.put("PendienteAsignacionAdmin", "Asignado");
                                    object.saveInBackground(new SaveCallback() {
                                        @Override
                                        public void done(ParseException e) {
                                            if (e == null) {
                                                Toast.makeText(getApplicationContext(), "Test drive asignado a:" + asesorNombre, Toast.LENGTH_LONG).show();
                                            } else {
                                                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();

                                            }
                                        }
                                    });

                                }

                            });

                            adapter.notifyDataSetChanged();

                        }
                    });

                    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });


                }

                @Override
                public void onLongClick(View view, int position) {

                }
            }));


            list.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            list.setHasFixedSize(true);

            ListAsesorAdminAsignaTestDrive adapter = new ListAsesorAdminAsignaTestDrive(getApplicationContext(), asesores);
            list.setAdapter(adapter);


            alertDialog.show();


        }
    }));

如果您不知道正确答案或解决此问题的方法,请感谢您不回答。

更新: Android 疯了!!!。当我第一次 onLongClick 时,警报对话框仍然不起作用,因为我没有办法返回,我在警报对话框的外部单击,它消失了。然后我再长按一下,它就起作用了!!!这很奇怪为什么会这样??????什么都没有改变。

标签: androidandroid-alertdialog

解决方案


使用这样的代码,

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setMessage(message);
    builder.setCancelable(false);
    builder.setPositiveButton(
            "Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();

                }
            });

    builder.setNegativeButton(
            "No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();

              }
            });

    AlertDialog alert = builder.create();
    alert.setOnShowListener(arg0 -> {
        alert.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.RED));
        alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.RED));
    });
    alert.show();

推荐阅读