【问题标题】:Add N Number of Radio buttons in Android在 Android 中添加 N 个单选按钮
【发布时间】:2021-01-16 17:42:07
【问题描述】:

我想根据一些值添加单选按钮。值定义了我必须显示的单选按钮的总数。目前我正在动态添加两个单选按钮,但这不是我添加单选按钮的正确解决方案。如果我必须为此代码显示 10 个单选按钮,我必须创建 10 个单选按钮实例。谁能建议我如何实现这一目标。

代码:-

class FragmentQues : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {

    return inflater.inflate(R.layout.fragmentques_layout, container, false)
}

@SuppressLint("ResourceType")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    // Create RadioButton programmatically
    val radioButton1 = RadioButton(activity)
    radioButton1.layoutParams= LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
    radioButton1.setText("No")
    radioButton1.id = 1

    val radioButton2 = RadioButton(activity)
    radioButton2.layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
    radioButton2.setText("Yes")
    radioButton2.id = 2

        profile_radio_group.addView(radioButton1)
        profile_radio_group.addView(radioButton2)

        profile_radio_group.setOnCheckedChangeListener { group, checkedId ->

            if (checkedId ==1){
                // Some code 
            }else{
                 // Some code 
            }
        }
}

}

【问题讨论】:

  • 你可以G。使用循环并将单选按钮对象存储在数组中。
  • 我正在尝试这个,你能给我一些例子吗。

标签: java android kotlin radio-button radio-group


【解决方案1】:

嗯,这可以通过一个简单的 for 循环来完成

class FragmentQues : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragmentques_layout, container, false)
    }

    @SuppressLint("ResourceType")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val value = 2;
        // If you have custom text for each button you have to define them in a list
        val textList = listOf("No", "Yes")
        
        for(i in 0 until value){
            // Create RadioButton programmatically
            val radioButton = RadioButton(activity)
            radioButton.layoutParams= LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT)

            radioButton.setText(textList[i])
            radioButton.id = i

            profile_radio_group.addView(radioButton)
        }
        

        profile_radio_group.setOnCheckedChangeListener { group, checkedId ->

            if (checkedId ==1){
                // Some code 
            }else{
                // Some code 
            }
        }
    }
  • 请注意,文本必须作为数组传递,以满足您的需求,如代码中所述

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-10
    • 2013-10-10
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 2013-08-27
    • 2012-09-26
    • 1970-01-01
    相关资源
    最近更新 更多