在Android中添加N个单选按钮。

huangapple go评论64阅读模式
英文:

Add N Number of Radio buttons in Android

问题

我想根据某个值添加单选按钮该值定义了我需要显示的单选按钮总数目前我正在动态添加两个单选按钮但这对于我来说不是一个合适的解决方案因为我需要添加单选按钮如果我需要为这段代码显示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)

        // 动态创建单选按钮
        val radioButton1 = RadioButton(activity)
        radioButton1.layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT)
        radioButton1.setText("否")
        radioButton1.id = 1

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

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

        profile_radio_group.setOnCheckedChangeListener { group, checkedId ->

            if (checkedId == 1){
                // 某些代码
            } else {
                // 某些代码
            }
        }
    }
}
英文:

I want to add radio buttons based on some value. Value defines the total number of radio button that i have to show. Currently i am adding two radio button dynamically but this is not a proper solution for me to adding the Radio buttons. If i have to show 10 radio buttons for this code i have to create 10 instances of radio buttons. Can anyone please suggest me how can i achieve this.

code:-

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 
            }
        }
}

}

答案1

得分: 1

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;
        // 如果您对每个按钮都有自定义文本,您需要在列表中定义它们
        val textList = listOf("No", "Yes")

        for (i in 0 until value) {
            // 以编程方式创建单选按钮
            val radioButton = RadioButton(activity)
            radioButton.layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT)

            radioButton.text = textList[i]
            radioButton.id = i

            profile_radio_group.addView(radioButton)
        }

        profile_radio_group.setOnCheckedChangeListener { group, checkedId ->

            if (checkedId == 1) {
                // 某些代码
            } else {
                // 某些代码
            }
        }
    }
}
  • 请注意,根据代码中的描述,文本必须作为数组传递以匹配您的需求
英文:

Well, This can be done through a simple for loop

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 
            }
        }
    }
  • Note that the text has to be passed as an array to match your needs as described in the code

huangapple
  • 本文由 发表于 2020年10月1日 09:36:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64147945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定