Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android单选按钮ID更改_Android_Radio Button - Fatal编程技术网

android单选按钮ID更改

android单选按钮ID更改,android,radio-button,Android,Radio Button,我的应用程序中有3个单选按钮,我正在获取所选单选按钮的ID,如下所示: int selectedId = radioGroup.getCheckedRadioButtonId(); 然后在我的if条件下,我比较了它: if( selectedId == 2131230992 { do something...} else if( selectedId == 2131230993{ do something...} else {...} 然而,这是工作得很好,但突然我注意到在我的生

我的应用程序中有3个单选按钮,我正在获取所选单选按钮的ID,如下所示:

  int selectedId = radioGroup.getCheckedRadioButtonId();
然后在我的if条件下,我比较了它:

  if( selectedId == 2131230992  { do something...}
 else if( selectedId == 2131230993{ do something...}
else {...}
然而,这是工作得很好,但突然我注意到在我的生活应用程序,单选按钮不再工作

我调试了它,发现当我记录radiobuttons ID的值时,它现在和以前不同了

Log.e("ID", String.valueOf(selectedId));
现在是2131231081、2131231081、2131231083等等

这些ID是如何变化的,为什么?在我的应用程序构建期间,这些ID是固定的数字,所以我在我的条件下使用它们,但现在有了完全不同的ID


那么,如果不能以这种方式使用ID,那么检查选中哪个单选按钮的正确方法应该是什么呢?

您不能直接使用资源ID,因为它们可能在任何构建时都会更改。您必须在运行时使用“Context.getResources().getIdentifier(…)”根据您要查找的资源类型将它们的名称解析为它们的ID

正确的方法是(伪代码):

其中“radiobutton1”是其内部的一个(在XML布局文件中)。

请检查:

->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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:id="@+id/rg_test"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RadioButton
            android:id="@+id/rb_one"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Radiobutton One" />

        <RadioButton
            android:id="@+id/rb_two"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Radiobutton Two" />
    </RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>

->科特林:

        // Getting the radio group from the layout
        val rg = findViewById<RadioGroup>(R.id.rg_test)

        // Initializing the radio button check change listener
        rg.setOnCheckedChangeListener { radioGroup, selectedId ->

            // Now, listening to the changed radio button here
            when (selectedId) {

                // Case 1
                R.id.rb_one -> {
                    // do something
                }

                // Case 2
                R.id.rb_two -> {
                    // do something
                }
            }
        }
//从布局中获取无线组
val rg=findViewById(R.id.rg_测试)
//初始化单选按钮检查更改侦听器
rg.setOnCheckedChangeListener{radioGroup,选择EDID->
//现在,在这里收听更改的单选按钮
何时(选择EDID){
//案例1
R.id.rb\U一->{
//做点什么
}
//案例2
R.id.rb_二->{
//做点什么
}
}
}

为什么您没有在xml文件上设置id而不是使用此数字来解释,这是有道理的,所以我没有意识到id正在更改每个版本。现在我明白了。
        // Getting the radio group from the layout
        val rg = findViewById<RadioGroup>(R.id.rg_test)

        // Initializing the radio button check change listener
        rg.setOnCheckedChangeListener { radioGroup, selectedId ->

            // Now, listening to the changed radio button here
            when (selectedId) {

                // Case 1
                R.id.rb_one -> {
                    // do something
                }

                // Case 2
                R.id.rb_two -> {
                    // do something
                }
            }
        }