mardi 16 juin 2020

Save state from checkbox in custom Dialog

Guys I have create an Custom dialog with one edit text and one checkbox with this XML code

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dialog_rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity"
android:background="#000000"
>

<TextView
    android:id="@+id/dialog_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:padding="5dp"
    android:text="Velocidade de Trabalho"
    android:textColor="#ffffff"
    android:textSize="32dp" />

<EditText
    android:id="@+id/et_name"
    android:layout_width="946dp"
    android:layout_height="105dp"
    android:layout_below="@id/dialog_title"
    android:fontFamily="@font/digital"
    android:hint="Digite a Taxa Desejada"
    android:inputType="number"
    android:maxLength="3"
    android:textColor="#ffffff"
    android:textSize="50dp"
    tools:ignore="HardcodedText" />

<CheckBox
    android:id="@+id/checkbox"
    style="?android:attr/textAppearanceMedium"
    android:layout_width="1000dp"
    android:layout_height="71dp"
    android:text="Habilitar GPS"
    android:textColor="#ffffff"
    android:textSize="32dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="172dp" />

<Button
    android:id="@+id/dialog_negative_btn"
    android:layout_width="220dp"
    android:layout_height="110dp"
    android:layout_below="@+id/et_name"
    android:layout_centerVertical="true"
    android:layout_marginTop="92dp"
    android:layout_marginEnd="10dp"
    android:layout_marginRight="10dp"
    android:layout_toStartOf="@+id/dialog_positive_btn"
    android:layout_toLeftOf="@id/dialog_positive_btn"
    android:background="#ffffff"
    android:text="CANCELAR"
    android:textSize="22dp" />

<Button
    android:id="@+id/dialog_positive_btn"
    android:layout_width="220dp"
    android:layout_height="110dp"
    android:layout_below="@id/et_name"
    android:layout_alignEnd="@+id/et_name"
    android:layout_centerVertical="true"
    android:layout_marginTop="90dp"
    android:layout_marginEnd="1dp"
    android:background="#ffffff"
    android:text="CONFIRMAR"
    android:textSize="22dp" />

in my fragment I created a method to show the dialog box, this dialog box is shown on click, after displaying the box I check the option in my checkbox and confirm using the positive button but when clicking again to show my dialogbox o checkbox state is not saved how can I save this state?

this is my method in fragment

 private void showdialogGPS (){

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.dialog_gps,null);

    // Specify alert dialog is not cancelable/not ignorable
    builder.setCancelable(false);

    // Set the custom layout as alert dialog view
    builder.setView(dialogView);

    // Get the custom alert dialog view widgets reference
    Button btn_positive =  dialogView.findViewById(R.id.dialog_positive_btn);
    Button btn_negative =  dialogView.findViewById(R.id.dialog_negative_btn);
    CheckBox btn_Habilita = dialogView.findViewById(R.id.checkbox);
    final EditText et_name = dialogView.findViewById(R.id.et_name);

    btn_Habilita.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            if (isChecked){
                  habilitagps = true;
       }
            else {
                habilitagps = false;
            }
                        }
    });

    // Create the alert dialog
    final AlertDialog dialog = builder.create();

    btn_positive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Dismiss the alert dialog
            dialog.cancel();
            String name = et_name.getText().toString();
            Toast.makeText(getActivity(),
                "Velocidade de Trabalho : " + name +  ("\u2705"), Toast.LENGTH_LONG).show();
            // Say hello to the submitter
            Txaplicacao.setText(name);
             if (habilitagps){
                 Toast.makeText(getActivity(),
                     "GPS Habilitado " +  ("\u2705"), Toast.LENGTH_LONG).show();
             }else{
                 Toast.makeText(getActivity(),
                     "GPS Desabilitado " +  ("\u2705"), Toast.LENGTH_LONG).show();
             }
        }
    });

    // Set negative/no button click listener
    btn_negative.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Dismiss/cancel the alert dialog
            //dialog.cancel();
            dialog.dismiss();
            Toast.makeText(getActivity(),
                "Valor Não Alterado" + ("\u274c"), Toast.LENGTH_LONG).show();
        }
    });

    dialog.show();
}



Aucun commentaire:

Enregistrer un commentaire