[英]JAVA - RadioButton checked
我的應用程序有一個問題。 我有兩個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 button = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
if(BouttonRadio_B.isChecked()) {
button.setEnabled(false);
}
if(BouttonRadio_R.isChecked()) {
button.setEnabled(true);
}
必須工作
Button button = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
if(BouttonRadio_B.isChecked()) {
button.setEnabled(false);
}
if(BouttonRadio_R.isChecked()) {
button.setEnabled(true);
}
顯示對話后是否執行此代碼
alertDialog.show();
您必須實現偵聽器以偵聽RadioButton
的檢查更改偵聽器。 請參閱此文檔以獲取更多詳細信息。是的,您可以使用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);
}
}
這應該為您工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.