gpt4 book ai didi

JAVA - 单选按钮已选中

转载 作者:行者123 更新时间:2023-12-02 03:45:58 25 4
gpt4 key购买 nike

我的应用程序有一个问题。我有两个 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>

而且,我有一个 AlertDialog,它有三个按钮:

    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();


}

最后,我想检索选中的单选按钮,并且仅当它是蓝牙时,禁用中性按钮。为此,我找到了这个,但没有用:

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

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

最佳答案

您必须实现监听器来监听RadioButton的检查更改监听器。请参阅this documentation了解更多详细信息。是的,您可以使用 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);
}
}

这应该适合你。

关于JAVA - 单选按钮已选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36289858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com