如何检查一排按钮中是否至少有一个 ToggleButton 被选中?

发布于 2025-01-16 21:48:33 字数 1684 浏览 2 评论 0原文

我正在致力于创建一个应用程序,该应用程序除其他功能外还具有集成的GAD 测试功能(用于计算和测量用户压力水平的自测)。它看起来是这样的:

GAD Test

它由一个表格组成,其中包含多行切换按钮。作为示例,这是其中 1 个按钮的代码:

<ToggleButton
    android:id="@+id/row1_btn4"
    android:layout_width="200px"
    android:layout_height="60dp"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/button_border"
    android:gravity="center"
    android:paddingStart="10px"
    android:paddingEnd="10px"
    android:scaleX="0.5"
    android:scaleY="0.65"
    android:textColor="@color/white"
    android:textOff="   "
    android:textOn="✓"
    android:textSize="28sp" />

这是检查按钮是否被选中的代码:

row1_btn4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            gadpoints += 3;
            ((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
        } else if (!isChecked) {
            gadpoints -= 3;
            ((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
        } else {
            gadpoints += 0;
            ((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
        }
    }
});

一切都按预期工作,如果 ToggleButton 被选中,则用户被授予给定的积分。但是,我想实现两件事:

a)使其只能检查每行中的1个按钮,并防止用户检查同一行中的另一个按钮(如果他/她已经)检查1

b)检查在一行按钮中是否没有一个被检查,如果是,则通知用户

我想不出可行的解决方案,因为我本质上将检查按钮是否未被检查,但话又说回来,其中一些是不应该受到检查的。有什么想法吗?

I'm working on creating an application, which amongst other features has an integrated GAD Test functionality (self test to calculate and measure the user's stress level). This is how it looks:

GAD Test

It is consisted of a Table, with multiple rows of ToggleButtons. This is the code for 1 of the buttons, as an example:

<ToggleButton
    android:id="@+id/row1_btn4"
    android:layout_width="200px"
    android:layout_height="60dp"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/button_border"
    android:gravity="center"
    android:paddingStart="10px"
    android:paddingEnd="10px"
    android:scaleX="0.5"
    android:scaleY="0.65"
    android:textColor="@color/white"
    android:textOff="   "
    android:textOn="✓"
    android:textSize="28sp" />

and this is the code to check whether a button is checked or not:

row1_btn4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            gadpoints += 3;
            ((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
        } else if (!isChecked) {
            gadpoints -= 3;
            ((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
        } else {
            gadpoints += 0;
            ((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
        }
    }
});

Everything is working as it should, if a ToggleButton is checked, the user is awarded the given points. However, I would like to implement 2 things:

a) Make it so that only 1 button from each row can be checked, and prevent the user from checking another one from the same row if he / she already checked 1

b) Check whether in a row of buttons none of them has been checked, and if so, notify the user

I cannot think of a feasible solution to this, because I will be essentially checking if a button hasn't been checked, but then again, some of them are meant to be unchecked. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹎☆浅夏丿初晴 2025-01-23 21:48:33

您可以使用 RadioGroup 来管理每行仅选择一个条目。要在未选择任何内容的情况下启动 RadioGroup,您可以对其调用 clearCheck()

要确保在单击“提交”时在所有行中进行选择,您可以在每个组/行上调用 getCheckedRadioButtonId(),如果未选择任何内容,它将返回 -1。

如果您想自定义单选按钮的外观,有很多关于如何执行此操作的示例 这里

下面是 onCreate 中的示例:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    List<RadioGroup> rows = Arrays.asList(
        findViewById(R.id.row1),
        findViewById(R.id.row2)
    );

    // Start out with all buttons un-checked
    for(RadioGroup row : rows) {
        row.clearCheck();
    }

    // make a list of button IDs for each row to 
    // simplify calculating the score
    List<List<Integer>> rowIds = Arrays.asList(
        Arrays.asList(R.id.row1_btn1, R.id.row1_btn2, R.id.row1_btn3, R.id.row1_btn4),
        Arrays.asList(R.id.row2_btn1, R.id.row2_btn2, R.id.row2_btn3, R.id.row2_btn4)
    );

    Button submit = findViewById(R.id.submit);

    submit.setOnClickListener(v -> {
        // check that all rows have selections
        boolean missing_values = false;
        for(RadioGroup row : rows) {
            // This will be -1 if nothing was selected
            if( row.getCheckedRadioButtonId() == -1 ) {
                missing_values = true;
            }
        }

        if( missing_values ) {
            Toast.makeText(this, "Missing entries on some rows", Toast.LENGTH_LONG).show();
        }
        else {
            // handle the result - add up scores or whatever is needed and send the data
            // to the model
            int score = 0;
            for(int r = 0; r < rows.size(); ++r) {
                int sel = rows.get(r).getCheckedRadioButtonId();
                score += rowIds.get(r).indexOf(sel);
            }
            Toast.makeText(this, "All rows selected - score = " + score, Toast.LENGTH_LONG).show();
        }
    });
}

以及随之而来的 XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/row1_title"
        android:text="Row 1"
        android:layout_margin="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/row1"
        app:layout_constraintBottom_toBottomOf="@id/row1"/>

    <RadioGroup
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/row1"
        android:orientation="horizontal"
        app:layout_constraintStart_toEndOf="@id/row1_title"
        app:layout_constraintTop_toTopOf="parent">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row1_btn1"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row1_btn2"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row1_btn3"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row1_btn4"/>

    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/row2_title"
        android:text="Row 2"
        android:layout_margin="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/row2"
        app:layout_constraintBottom_toBottomOf="@id/row2"/>

    <RadioGroup
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/row2"
        android:orientation="horizontal"
        app:layout_constraintStart_toEndOf="@id/row2_title"
        app:layout_constraintTop_toBottomOf="@id/row1">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row2_btn1"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row2_btn2"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row2_btn3"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row2_btn4"/>

    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:id="@+id/submit"
        android:layout_margin="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/row2"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

You can manage having only one entry selected per row using a RadioGroup. To start the RadioGroup with nothing selected you can call clearCheck() on it.

To make sure that a selection was made in all rows when they click "Submit" you can call getCheckedRadioButtonId() on each group/row, it will return -1 if nothing was selected.

If you want to customize what the radio buttons look like there are lots of examples of how to do that here.

Here is an example of what that would look like in onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    List<RadioGroup> rows = Arrays.asList(
        findViewById(R.id.row1),
        findViewById(R.id.row2)
    );

    // Start out with all buttons un-checked
    for(RadioGroup row : rows) {
        row.clearCheck();
    }

    // make a list of button IDs for each row to 
    // simplify calculating the score
    List<List<Integer>> rowIds = Arrays.asList(
        Arrays.asList(R.id.row1_btn1, R.id.row1_btn2, R.id.row1_btn3, R.id.row1_btn4),
        Arrays.asList(R.id.row2_btn1, R.id.row2_btn2, R.id.row2_btn3, R.id.row2_btn4)
    );

    Button submit = findViewById(R.id.submit);

    submit.setOnClickListener(v -> {
        // check that all rows have selections
        boolean missing_values = false;
        for(RadioGroup row : rows) {
            // This will be -1 if nothing was selected
            if( row.getCheckedRadioButtonId() == -1 ) {
                missing_values = true;
            }
        }

        if( missing_values ) {
            Toast.makeText(this, "Missing entries on some rows", Toast.LENGTH_LONG).show();
        }
        else {
            // handle the result - add up scores or whatever is needed and send the data
            // to the model
            int score = 0;
            for(int r = 0; r < rows.size(); ++r) {
                int sel = rows.get(r).getCheckedRadioButtonId();
                score += rowIds.get(r).indexOf(sel);
            }
            Toast.makeText(this, "All rows selected - score = " + score, Toast.LENGTH_LONG).show();
        }
    });
}

and the XML that goes along with it

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/row1_title"
        android:text="Row 1"
        android:layout_margin="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/row1"
        app:layout_constraintBottom_toBottomOf="@id/row1"/>

    <RadioGroup
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/row1"
        android:orientation="horizontal"
        app:layout_constraintStart_toEndOf="@id/row1_title"
        app:layout_constraintTop_toTopOf="parent">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row1_btn1"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row1_btn2"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row1_btn3"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row1_btn4"/>

    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/row2_title"
        android:text="Row 2"
        android:layout_margin="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/row2"
        app:layout_constraintBottom_toBottomOf="@id/row2"/>

    <RadioGroup
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/row2"
        android:orientation="horizontal"
        app:layout_constraintStart_toEndOf="@id/row2_title"
        app:layout_constraintTop_toBottomOf="@id/row1">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row2_btn1"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row2_btn2"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row2_btn3"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/row2_btn4"/>

    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:id="@+id/submit"
        android:layout_margin="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/row2"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

不要使用切换按钮,而是为每行使用 Radio Group
然后,您可以为单选按钮设计一个自定义可绘制对象,使它们看起来像您当前拥有的那样。

使用此创建自定义按钮

<?xml version="1.0" encoding="utf-8"?>
<item android:state_checked="false" android:drawable="@drawable/ic_box"/>
<item android:state_checked="true" android:drawable="@drawable/ic_check"/>

您可以从 android studio 中的资源管理器获取 Box 和 Check 可绘制对象。

Instead of using toggle buttons, use a Radio Group for each row.
Then you can design a custom drawable for Radio Buttons to make them look like what you currently have.

Create custom button using this

<?xml version="1.0" encoding="utf-8"?>
<item android:state_checked="false" android:drawable="@drawable/ic_box"/>
<item android:state_checked="true" android:drawable="@drawable/ic_check"/>

You can get Box and Check drawable from Resource Manager in android studio.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文