Изменить текст положительной кнопки диалогового окна с предупреждением после его отображения - PullRequest
0 голосов
/ 04 мая 2020

У меня есть диалоговое окно оповещения с вращением в нем. Мне нравится изменять текст положительной кнопки в зависимости от положения счетчика.

mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

if (position == 0) {

/////
// I have access to the dialog object here and need to change the positive button text 
////

}


@Override
public void onNothingSelected(AdapterView<?> parentView) {
          // your code here
}

});

1 Ответ

0 голосов
/ 04 мая 2020

Вот как я это сделал.

Объявленные кнопки My Class

MaterialButton positiveBtn,negativeBtn;

Создал мою функцию и инициализировал мои кнопки

void alertDialog(){
        AlertDialog.Builder dialogBuilder = new 
        AlertDialog.Builder(requireActivity());
        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.test_layout, null);
        dialogBuilder.setView(dialogView);


        positiveBtn = dialogView.findViewById(R.id.btn_positive);
        negativeBtn = dialogView.findViewById(R.id.btn_negative);

        AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();


        negativeBtn.setOnClickListener(v -> {

            positiveBtn.setText("Text 2 Positive ");

        });


    }

Вот test_layout

<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_margin="16dp"
    android:layout_height="wrap_content">

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btn_positive"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text 1 Positive "
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent">


    </com.google.android.material.button.MaterialButton>

    <com.google.android.material.button.MaterialButton
        app:layout_constraintTop_toBottomOf="@id/btn_positive"
        android:id="@+id/btn_negative"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Cancel "
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">


    </com.google.android.material.button.MaterialButton>

</androidx.constraintlayout.widget.ConstraintLayout>

В вашем В сценарии у вас уже есть пользовательский макет для Alert Dialog, просто объявляйте переменные, инициализируйте и устанавливайте Text внутри индекса счетчика.

этот ответ был вдохновлен здесь

...