自定义对话框内消失的字段

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

我试图用Dialog,2 xImageViews,3 xTextViews和2xRadioButtons创建自定义Button。当然我不能使它成为常规的Dialog(因为使用了各种各样的Views),所以我决定创建自定义layoutClass,它将负责设置它。一切似乎都很好,直到我建造它。 对话显示只有ImageView和第一个TextView但其他Views消失了。 DialogBox 应该是这样的: enter image description here 多数民众赞成我的代码 - 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="@color/dark_site_gray">

    <Button
        android:id="@+id/button4"
        android:layout_width="172dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="12dp"
        android:background="@color/igloo_theme_color"
        android:text="OK"
        android:textColor="@color/mdtp_white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/radioButton4" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@color/burgrundy" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="327dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:gravity="center_horizontal"
        android:text="TextView"
        android:textColor="@color/mdtp_white"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView2" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:gravity="center_horizontal"
        android:text="TextView"
        android:textColor="@color/mdtp_white"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:buttonTint="@color/mdtp_white"
        android:text="Akceptuj"
        android:textColor="@color/mdtp_white"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:buttonTint="@color/mdtp_white"
        android:text="Odrzuć"
        android:textColor="@color/mdtp_white"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioButton" />

    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:buttonTint="@color/mdtp_white"
        android:text="Przekaż dalej"
        android:textColor="@color/mdtp_white"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioButton2" />

    <Button
        android:id="@+id/button"
        android:layout_width="172dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="12dp"
        android:background="@color/igloo_theme_color"
        android:text="Anuluj"
        android:textColor="@color/mdtp_white"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioButton4" />

</android.support.constraint.ConstraintLayout>

类:

public class DialogCreator extends Dialog {
    public DialogCreator(@NonNull Context context) {
        super(context);
    }

    public void showSwitchDialog(Context context, String taskID, boolean isCoordinator, View.OnClickListener posButtonClickListener){
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);

        dialog.setContentView(R.layout.dialog_switch_service);
        TextView task = dialog.findViewById(R.id.textView);
        TextView chooseOptionText = dialog.findViewById(R.id.textView2);
        Button cancelButton = dialog.findViewById(R.id.button);
        Button okButton = dialog.findViewById(R.id.button4);
        RadioButton redirect = dialog.findViewById(R.id.radioButton4);

        if(!isCoordinator) redirect.setVisibility(View.GONE);
        okButton.setOnClickListener(posButtonClickListener);

        task.setText("ID: " + taskID);
        chooseOptionText.setText("Choose option:");
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();

    }
}

方法调用(对话框显示):

DialogCreator dc = new DialogCreator(MainScreenActivity.this);
                dc.showSwitchDialog(MainScreenActivity.this, "000000", true,  new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RadioButton radioAccept = v.findViewById(R.id.radioButton);
                        RadioButton radioDecline = v.findViewById(R.id.radioButton2);
                        RadioButton radioRedirect = v.findViewById(R.id.radioButton4);

                        if(radioAccept.isChecked()){
                            Toast.makeText(MainScreenActivity.this, "Accept", Toast.LENGTH_SHORT).show();
                        }
                        if(radioDecline.isChecked()){
                            Toast.makeText(MainScreenActivity.this, "Decline", Toast.LENGTH_SHORT).show();
                        }
                        if(radioRedirect.getVisibility() != View.GONE){
                            if (radioRedirect.isChecked()){
                                Toast.makeText(MainScreenActivity.this, "Redirect", Toast.LENGTH_SHORT).show();
                            }
                        }

                    }
                });

一切似乎都很好(以我的拙见),但我不知道是否有什么不对或丢失,或者我不能用这种方式使用Dialog。如果这是正确的方法,那对我来说会很好,因为我可以用我的自定义版本替换我之前使用过的所有Dialogs,这会让app变得更好,更清晰。

android android-layout android-dialog customdialog
2个回答
2
投票

那么你可以使用DialogFragment

DialogCreator.java

public class DialogCreator extends DialogFragment {

    private static final String TASK_ID_KEY = "taskID";
    private static final String COORDINATOR_KEY = "isCoordinator";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(false);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.FullScreenDialogStyle);
    }

    public static DialogCreator newInstance(String taskID, boolean isCoordinator) {
        DialogCreator creator = new DialogCreator();
        Bundle bundle = new Bundle();
        bundle.putString(TASK_ID_KEY, taskID);
        bundle.putBoolean(COORDINATOR_KEY, isCoordinator);
        creator.setArguments(bundle);
        return creator;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TextView task = view.findViewById(R.id.textView);
        TextView chooseOptionText = view.findViewById(R.id.textView2);
        Button cancelButton = view.findViewById(R.id.button);
        Button okButton = view.findViewById(R.id.button4);
        final RadioButton radioAccept = view.findViewById(R.id.radioButton);
        final RadioButton radioDecline = view.findViewById(R.id.radioButton2);

        final RadioButton redirect = view.findViewById(R.id.radioButton4);

        Bundle bundle = getArguments();
        if (bundle == null) return;
        if (!bundle.containsKey(TASK_ID_KEY) || !bundle.containsKey(COORDINATOR_KEY)) return;

        boolean isCoordinator = bundle.getBoolean(COORDINATOR_KEY);
        String taskID = bundle.getString(TASK_ID_KEY);
        if (!isCoordinator) redirect.setVisibility(View.GONE);

        task.setText("ID: " + taskID);
        chooseOptionText.setText("Choose option:");
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (radioAccept.isChecked()) {
                    Toast.makeText(requireContext(), "Accept", Toast.LENGTH_SHORT).show();
                }
                if (radioDecline.isChecked()) {
                    Toast.makeText(requireContext(), "Decline", Toast.LENGTH_SHORT).show();
                }
                if (redirect.getVisibility() != View.GONE) {
                    if (redirect.isChecked()) {
                        Toast.makeText(requireContext(), "Redirect", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        if (dialog.getWindow() != null) {
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }
        return dialog;
    }
}

styles.xml中的FullScreenDialogStyle

<style name="FullScreenDialogStyle" parent="Theme.AppCompat.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">false</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowBackground">@android:color/white</item>
</style>

在您的活动中:

DialogCreator.newInstance("taskID",true).show(getSupportFragmentManager(),"TAG")

0
投票

尝试阅读this答案,了解如何使用自定义布局创建对话框。您正在使用函数showSwitchDialog来创建对话框,但我认为您应该从扩展Dialog类的类创建对话框,然后使用以下内容显示它:

CustomDialogClass cdd = new CustomDialogClass();
cdd.show();

从上面的链接。我尝试使用LinearLayout根标记​​重写您的布局文件,这可能是显示其他视图的问题。如果它没有解决问题,请尝试调整对话框的创建,如上面的答案所示。

<?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="@color/dark_site_gray"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="16dp"
        app:srcCompat="@color/burgrundy" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="327dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:gravity="center_horizontal"
        android:text="TextView"
        android:textColor="@color/mdtp_white"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:gravity="center_horizontal"
        android:text="TextView"
        android:textColor="@color/mdtp_white"
        android:textSize="18sp" />

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:buttonTint="@color/mdtp_white"
        android:text="Akceptuj"
        android:textColor="@color/mdtp_white" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:buttonTint="@color/mdtp_white"
        android:text="Odrzuć"
        android:textColor="@color/mdtp_white" />

    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:buttonTint="@color/mdtp_white"
        android:text="Przekaż dalej"
        android:textColor="@color/mdtp_white" />

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

        <Button
            android:id="@+id/button"
            android:layout_width="172dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_marginStart="8dp"
            android:layout_marginTop="12dp"
            android:background="@color/igloo_theme_color"
            android:text="Anuluj"
            android:textColor="@color/mdtp_white" />

        <Button
            android:id="@+id/button4"
            android:layout_width="172dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_marginStart="8dp"
            android:layout_marginTop="12dp"
            android:layout_marginEnd="8dp"
            android:background="@color/igloo_theme_color"
            android:text="OK"
            android:textColor="@color/mdtp_white" />
    </RelativeLayout>

</LinearLayout>

希望它有效!

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