广播按钮setOnClickListener无法完全运行

发布于 2025-02-09 05:30:45 字数 539 浏览 0 评论 0原文

我正在开发一个移动应用程序,其中使用了无线电按钮。奇怪的是,无线电按钮的SetOnClickListener无法运行完整的代码。这是我的广播按钮的代码,

radioButtonWeekly.setOnClickListener {
            btnSelectWeek.isEnabled = true
            btnSelectWeek.setBackgroundColor(Color.WHITE)
            btnSelectMonth.isEnabled = false
            btnSelectMonth.setBackgroundColor(Color.GRAY)

            Log.i("${PerformanceFragment::class.java.simpleName}: ", "Selected Week No: ${selectedWeekNo}")
            }

仅在上面的四个语句运行。最后一个语句(log.i)不运行。实际上,上面的任何四个语句都不运行。请说明发生了什么事?

I am working on a mobile app in which I have used the radio buttons. The strange thing is that the radio button's setOnClickListener does not run complete code. Here is the code of my radio Button

radioButtonWeekly.setOnClickListener {
            btnSelectWeek.isEnabled = true
            btnSelectWeek.setBackgroundColor(Color.WHITE)
            btnSelectMonth.isEnabled = false
            btnSelectMonth.setBackgroundColor(Color.GRAY)

            Log.i("${PerformanceFragment::class.java.simpleName}: ", "Selected Week No: ${selectedWeekNo}")
            }

Only the above four statements run. The last statement (Log.i) does not run. In fact, any statement below the above four statements does not run. Please explain what is going on wrong?

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

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

发布评论

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

评论(1

别在捏我脸啦 2025-02-16 05:30:45

首先创建无线电组

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton android:id="@+id/radio_pirates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pirates"
        android:onClick="onRadioButtonClicked"/>
    <RadioButton android:id="@+id/radio_ninjas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ninjas"
        android:onClick="onRadioButtonClicked"/>
</RadioGroup>

,然后用于手柄无线电按钮

fun onRadioButtonClicked(view: View) {
    if (view is RadioButton) {
        // Is the button now checked?
        val checked = view.isChecked

        // Check which radio button was clicked
        when (view.getId()) {
            R.id.radio_pirates ->
                if (checked) {
                    // Pirates are the best
                }
            R.id.radio_ninjas ->
                if (checked) {
                    // Ninjas rule
                }
        }
    }
}

https://develovelvelvementer.android.android。 com/guide/topics/ui/controls/radiobutton

first create radio group

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton android:id="@+id/radio_pirates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pirates"
        android:onClick="onRadioButtonClicked"/>
    <RadioButton android:id="@+id/radio_ninjas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ninjas"
        android:onClick="onRadioButtonClicked"/>
</RadioGroup>

then for handle radio buttons

fun onRadioButtonClicked(view: View) {
    if (view is RadioButton) {
        // Is the button now checked?
        val checked = view.isChecked

        // Check which radio button was clicked
        when (view.getId()) {
            R.id.radio_pirates ->
                if (checked) {
                    // Pirates are the best
                }
            R.id.radio_ninjas ->
                if (checked) {
                    // Ninjas rule
                }
        }
    }
}

source https://developer.android.com/guide/topics/ui/controls/radiobutton

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