有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何防止RadioGroup事件?

我有一个放射组

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

</RadioGroup>

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

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

        }
    }
);

共 (1) 个答案

  1. # 1 楼答案

    您可以使用警报对话框,以防用户接受,然后您进行无线电组选择,或者不进行其他操作。下面是一个例子:

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

    我希望它对你有用