【问题标题】:JAVA - RadioButton checkedJAVA - 选中单选按钮
【发布时间】:2016-07-17 08:04:44
【问题描述】:

我的申请有一个问题。我有两个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);
}

【问题讨论】:

  • 您在哪个类和方法中拥有您在最后提供的用于获取对话框按钮并检查单选按钮(两者之一)是否被选中然后启用/禁用按钮的代码遥控器的
  • @nits.kk : 我的类是 MainActivity.java,我的方法在 Mainactivity 中。

标签: java android radio-button


【解决方案1】:

它必须工作

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

如果显示对话框后执行此代码

alertDialog.show();

【讨论】:

  • Krumish :我对你的代码没意见。我在 alerttdialog.show() 之后添加了这段代码。该应用程序编译没有错误,但是当我单击该项目时,在我的菜单上,使用此方法的应用程序FC。
【解决方案2】:

您必须实现监听器来监听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);
        }
}

这应该适合你。

【讨论】:

  • 您能在回答中说明为什么这是解决问题的好方法吗?另外,你能做button.setEnabled(isChecked)吗? - 这将保存一个 if/else 结构。
  • 您必须实现监听器来监听 RadioButton 的检查更改监听器。有关更多详细信息,请参阅this。是的,您可以使用 button.setEnabled(isChecked)。如果有帮助请点赞。
  • 谢谢。我已经为您编辑了它,请愿意在将来自己这样做。 :-)
猜你喜欢
  • 1970-01-01
  • 2016-06-09
  • 2023-04-03
  • 2019-10-04
  • 1970-01-01
  • 2011-04-29
  • 2017-11-05
  • 2013-04-12
  • 2017-07-24
相关资源
最近更新 更多