英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论