【问题标题】:How can I prevent RadioGroup event?如何防止 RadioGroup 事件?
【发布时间】:2015-11-21 16:55:54
【问题描述】:

我有一个 RadioGroup

<RadioGroup
    android:id="@+id/typeGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <RadioButton
        android:id="@+id/type0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="type0" />
    <RadioButton
        android:id="@+id/type1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="type1" />

</RadioGroup>

这是程序,我可以要求用户确认更改,如果没有,单选按钮不应该更改,是否有javascript的preventDefault之类的功能?

typeGroup=(RadioGroup)findViewById(R.id.typeGroup);

typeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            type=group.indexOfChild((RadioButton) findViewById(checkedId));

            //dialog confirm change
            //if yes, keep going
            //if no, stop event

        }
    }
);

【问题讨论】:

    标签: java android radio-button android-radiogroup


    【解决方案1】:

    您可以使用 alertDialog,以防用户接受,然后您进行单选组选择,否则不进行任何操作。这是一个例子:

    RadioGroup radioGroup = new RadioGroup(getApplicationContext());
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(RadioGroup radioGroup, int i) {
                  AlertDialog.Builder builder = new AlertDialog.Builder(CompeticionActivityPartidoMain.this);
                  builder.setTitle("Dialog").setMessage("Are you sure?")
                      .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                           @Override
                           public void onClick(DialogInterface dialog, int which) {
                                  dialog.dismiss();
                                  radiogroup.clearCheck();
                                 }
                          })
                       .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                  // Here your code
                                  switch (type) {
                                       case 0:
                                            size=2;
                                            break;
                                       case 1:
                                            size=1;
                                            break;
                                  }
                               }
                         });
                    AlertDialog alert = builder.create();
                    alert.show();
                    }
                }); 
    

    希望对你有用。

    【讨论】:

    • 对不起,这没有用,我编辑了有问题的代码,我不想让大小混淆你,我需要停止更改事件的功能
    • 我刚试过typeGroup.clearCheck()group.clearCheck(),两者都会使应用程序崩溃。另外,从函数名称来看,我猜它会取消选中组中的所有单选按钮,这不是我想要的。
    • 您能否更具体地说明您遇到的异常情况?
    • 我认为它重复检查和取消检查,进程:com.recordbutton,PID:763 java.lang.StackOverflowError at android.widget.RadioGroup.check(RadioGroup.java:168) at android.widget。 RadioGroup.clearCheck(RadioGroup.java:209) at com.testradio.MainActivity$1.onCheckedChanged(MainActivity.java:25) at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
    猜你喜欢
    • 1970-01-01
    • 2019-10-31
    • 2013-02-18
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 2020-09-09
    • 2018-07-11
    相关资源
    最近更新 更多