简体   繁体   中英

JAVA - RadioButton checked

I have one problem with my application. I have two RadioButton :

<RadioGroup
    android:layout_width="148dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" >
<!-- android:buttonTint="@color/BouttonsRadio" --> 

    <RadioButton
        android:layout_width="137dp"
        android:layout_height="wrap_content"
        android:text="@string/Boutton_Bluetooth"
        android:id="@+id/BouttonRADIO_Bluetooth"
        android:layout_gravity="center_horizontal"
        android:textSize="20dp"
        android:textStyle="bold" />

    <RadioButton
        android:layout_width="137dp"
        android:layout_height="wrap_content"
        android:text="@string/Boutton_RS232"
        android:id="@+id/BouttonRADIO_RS232"
        android:layout_gravity="center_horizontal"
        android:textSize="20dp"
        android:textStyle="bold" />
</RadioGroup>

And, I have one AlertDialog , who have three buttons :

    public void fenetre_connexiondeconnexion() {

    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);


    final View view2 = View.inflate(MainActivity.this, R.layout.fenetre_connexion_B_R, null);


    // Titre de la fenêtre
    alertDialogBuilder.setTitle("Paramètres connexion");
    alertDialogBuilder.setIcon(R.drawable.logo_connecterdeconnecter);

    // set dialog message
    alertDialogBuilder
            .setMessage("Veuillez choisir le type de connexion :")
            .setCancelable(false);

    alertDialogBuilder.setView(view2);


    alertDialogBuilder.setPositiveButton("CONNEXION", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // if this button is clicked, close
            // current activity
            connexion_rs232();
        }
    });
    alertDialogBuilder.setNegativeButton("ANNULER", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // if this button is clicked, just close
            // the dialog box and do nothing
            dialog.cancel();
        }
    })
            .setNeutralButton("PARAMETRES", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    fenetre_parametres_rs232(view2);
                }
            });




    // Création de la fenêtre
    AlertDialog alertDialog = alertDialogBuilder.create();


    // Affichage de la fenêtre
    alertDialog.show();


}

Finally, and I will want to retrieve the radio button checked, and ONLY if it's the Bluetooth, disable the neutral Button . For this, I have found this, but didn't work :

Button button = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);

if(BouttonRadio_B.isChecked()) {
    button.setEnabled(false);
}
if(BouttonRadio_R.isChecked()) {
    button.setEnabled(true);
}

It's must work

    Button button = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
    if(BouttonRadio_B.isChecked()) {
        button.setEnabled(false);
    }
    if(BouttonRadio_R.isChecked()) {
        button.setEnabled(true);
    }

If this code will be executed after showing the dialogue

alertDialog.show();

You have to implement the listener to listen the check change listener of RadioButton . See this documentation for more details.And yes you can use button.setEnabled(isChecked) :

BouttonRadio_R.setOnCheckedChangeListener(new
    CompoundButton.OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    {
       if (isChecked) {
            button.setEnabled(true);
        }
       else{
            button.setEnabled(false);
        }
}

This should work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM