如何打开切换按钮之一

时间:2020-02-25 05:40:28

标签: android xml togglebutton interlocked

我希望另一个切换按钮在打开时关闭。

<item android:state_checked="true"
    android:drawable="@drawable/press"></item>
<item android:drawable="@drawable/nomal"></item>

这是我的切换按钮的xml代码。此代码是在“可绘制”中制作的。

2 个答案:

答案 0 :(得分:0)

您需要创建RadioGroup,并在其中添加2 RadioButtons和可绘制对象。结帐第二个链接,它将描述如何正确执行。 另外,您需要将可绘制的xml按更改为选中

答案 1 :(得分:0)

您可以使用下面的选择器作为ToggleButton的背景,当按钮更改其检查状态时,<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/ic_checked" android:state_checked="true" /> <item android:drawable="@drawable/ic_unchecked" android:state_checked="false" /> </selector> 会选择适当的可绘制对象

setChecked()

并将两个按钮都包装在RadioGroup中;然后在每次单击/单击按钮时循环遍历此组,然后使用<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <RadioGroup android:id="@+id/toggleGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:orientation="horizontal"> <ToggleButton android:id="@+id/toggle_btn_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/toggle_btn_selector" android:onClick="onToggle" android:textOff="" android:textOn="" /> <ToggleButton android:id="@+id/toggle_btn_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/toggle_btn_selector" android:onClick="onToggle" android:textOff="" android:textOn="" /> </RadioGroup> </LinearLayout> 方法对这些按钮求反。

布局

public class MainActivity extends AppCompatActivity {

    RadioGroup mGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGroup = findViewById(R.id.toggleGroup);
    }

    public void onToggle(View view) {

        ToggleButton checkedBtn = ((ToggleButton) view);

        for (int j = 0; j < mGroup.getChildCount(); j++) {
            ToggleButton toggleButton = (ToggleButton) mGroup.getChildAt(j);
            int id = toggleButton.getId();
            if (view.getId() == id)
                continue;
            toggleButton.setChecked(!checkedBtn.isChecked());
        }

    }
}

行为

function validTable() {
            $('#sample_editable_1 tr').each(function (i, row) {
                debugger;
                var $row = $(row);
                    $testCode = $row.find('input[name*="code"]'),
                    $testName = $row.find('input[name*="testname"]'),
                    $testStatus = $row.find('input[name*="status"]'),
                    $testCollectDate = $row.find('input[name*="collecttime"]'),
                    $testNotes = $row.find('input[name*="notes"]'),
                    $testSpecimenType = $row.find('input[name*="specimen"]'),
                    $testDx = $row.find('input[name*="diagnosis"]');



                if ($testCode.val() == "") {
                    alert("Enter Lab Test Code.");
                    return false;
                }
                if ($testName.val() == "") {
                    alert("Enter Lab Test Name.");

                    return false;
                }
                if ($testStatus.val() == "") {
                    alert("Enter Lab Test Status.");

                    return false;
                }
                if ($testCollectDate.val() == "") {
                    alert("Enter Lab Test Collection Date.");

                    return false;
                }
                if ($testNotes.val() == "") {
                    alert("Enter Lab Test Note.");
                    return false;
                }
                if ($testSpecimenType.val() == 0) {
                    alert("Plese Select Specimen Type.");

                    return false;
                }
                if ($testDx.val() == "") {
                    alert("Enter Diagnosis Code.");

                    return false;
                }


            });
        }