問題

我有一個簡單的自定義多選項對話方塊.如果專案數量很小,那麼對話方塊工作正常,並在底部顯示OK和Cancel按鈕.但是如果有很多專案(所以你必須滾動列表) – 沒有顯示按鈕.我已經搜尋了SO,因為我的問題沒有運氣.我在Android API 27 ... 29 – 這是一樣的.也許我在佈局屬性中遺漏了重要的東西等....

這是我的程式碼和xml:

 package ru.vitvlkv.myapp.gui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.Set;

import ru.vitvlkv.myapp.R;
import ru.vitvlkv.myapp.playlists.Exercise;

public class SelectExercisesDialog extends DialogFragment {
    private static final String TAG = SelectExercisesDialog.class.getSimpleName();

    private final String message;
    private final List<Exercise> exercises;
    private RecyclerView recyclerView;
    private final SelectExercisesDialog.OnOKClickListener onOKButtonClick;
    private final Set<String> selectedExercisesIds;

    public interface OnOKClickListener {
        void onClick(Set<String> exerciseIds);
    }

    public SelectExercisesDialog(String message, List<Exercise> exercises, Set<String> selectedExercisesIds,
                                 final SelectExercisesDialog.OnOKClickListener onButtonOKClick) {
        this.message = message;
        this.exercises = exercises;
        this.selectedExercisesIds = selectedExercisesIds;
        this.onOKButtonClick = onButtonOKClick;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.select_exercises_dialog, null);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(new AllExercisesAdapter(exercises, selectedExercisesIds));

        setRetainInstance(true);

        return new AlertDialog.Builder(getActivity())
            .setView(view)
            .setMessage(message)
            .setPositiveButton(R.string.dialog_button_ok, (DialogInterface dialog, int id) -> {
                onOKButtonClick.onClick(selectedExercisesIds);
            })
            .setNegativeButton(R.string.dialog_button_cancel, (DialogInterface dialog, int id) -> {
            })
            .create();
    }

    private static class AllExercisesAdapter extends RecyclerView.Adapter<ExerciseViewHolder> {
        private final List<Exercise> exercises;
        private final Set<String> checkedExercisesIds;

        public AllExercisesAdapter(List<Exercise> exercises, Set<String> checkedExercisesIds) {
            this.exercises = exercises;
            this.checkedExercisesIds = checkedExercisesIds;
        }

        @NonNull
        @Override
        public ExerciseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.select_exercise_item_view, parent, false);

            return new ExerciseViewHolder(view, checkedExercisesIds);
        }

        @Override
        public void onBindViewHolder(@NonNull ExerciseViewHolder holder, int position) {
            Exercise exercise = exercises.get(position);
            holder.setExercise(exercise);
        }

        @Override
        public int getItemCount() {
            return exercises.size();
        }
    }


    private static class ExerciseViewHolder extends RecyclerView.ViewHolder {
        private Exercise exercise = null;
        private final TextView textView;
        public final CheckBox checkBox;
        private final Set<String> checkedExercisesIds;

        public ExerciseViewHolder(@NonNull View view, Set<String> checkedExercisesIds) {
            super(view);
            this.checkedExercisesIds = checkedExercisesIds;
            textView = view.findViewById(R.id.textView);
            checkBox = view.findViewById(R.id.checkBox);
            checkBox.setOnCheckedChangeListener((compoundButton, isChecked) -> {
                if (isChecked) {
                    checkedExercisesIds.add(exercise.getId());
                } else {
                    checkedExercisesIds.remove(exercise.getId());
                }
            });
        }

        public void setExercise(Exercise exercise) {
            this.exercise = exercise;
            textView.setText(exercise.getName());
            checkBox.setChecked(checkedExercisesIds.contains(exercise.getId()));
        }
    }
}
 

佈局/ select_adist_dialog.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
 

佈局/ select_action_item_view.xml:

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_item_height"
    android:layout_marginLeft="@dimen/margin_medium"
    android:layout_marginRight="@dimen/margin_medium"
    android:gravity="center_vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/margin_medium"
            android:text="MyExercise"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>
 

活動類只是一個POJO:

 package ru.vitvlkv.myapp.playlists;

public class Exercise {
    private final String id;

    private final String name;

    private Exercise(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }
}

 

如何解決這個問題並使對話方塊在所有情況下顯示OK Cancel按鈕? 任何幫助表示讚賞.

謝謝!

附:對話方塊的螢幕截圖:

Dialog without buttons

  最佳答案

如果您正在使用對話方塊片段,如果您想新增ok並取消您有兩個選擇

  1. 從AlertDialog.Builder擴充套件類,並在建立時新增正和負按鈕處理程式和文字

關於程式碼的示例看起來像這樣

 class ExampleDialog(context: Context) : AlertDialog.Builder(context) {


private val view by lazy {
    LayoutInflater.from(context).inflate(R.layout.dialog_exmaple, null, false)
}

override fun setView(layoutResId: Int): AlertDialog.Builder {

    // do the recylceview init from the view code here 

    return super.setView(view)
}


override fun setPositiveButton(
    text: CharSequence?,
    listener: DialogInterface.OnClickListener?
): AlertDialog.Builder {
    return super.setPositiveButton(
        context.getText(R.string.action_ok)
    ) { p0, p1 ->


    }
}


override fun setNegativeButton(
    text: CharSequence?,
    listener: DialogInterface.OnClickListener?
): AlertDialog.Builder {
    return super.setNegativeButton(
        context.getText(R.string.action_cancel)
    ) { p0, p1 ->


    }
}
 

}

  • 注意我不喜歡這種方式

    1. 您可以在您的 rxml 中新增兩個按鈕到 recycleview 的底部,並像 ok 一樣將文字新增到它們中,並透過高階函式或透過介面取消和處理 onClick,我可以向您展示下面的示例

我更喜歡這個程式碼更乾淨,並在DialogFragment類中新增單擊監聽器

 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/listData"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@id/actionOk"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<com.google.android.material.button.MaterialButton
    android:id="@+id/actionOk"
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="@string/action_ok"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/listData" />

<com.google.android.material.button.MaterialButton
    android:id="@+id/actionCancel"
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="@string/action_cancel"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@id/actionOk"
    app:layout_constraintTop_toBottomOf="@id/listData" />
 


   override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = super.onCreateDialog(savedInstanceState)
    val root = ConstraintLayout(activity)
    root.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setContentView(root)
    dialog.window?.let {
        it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        it.setWindowAnimations(R.style.DialogAnimationNormal)
    }
    dialog.setCanceledOnTouchOutside(true)
    return dialog
}
 

  相同標籤的其他問題

javaandroiddialog