Kotlin实现单选和多选

Kotlin实现单选和多选

2023年7月13日发(作者:)

Kotlin实现单选和多选最终效果:核⼼代码:object OrderViewCenter { /** * 创建RadioButton */ fun createTripRadioButton(context: Context, radioId: Int): RadioButton { val btn = RadioButton(context) = radioId val params = Params( _PARENT, _CONTENT) Params = params Drawable = null var drawable: Drawable = wable(or_radio) nds(0, 0, (24), (24)) poundDrawables(drawable, null, null, null) ndDrawablePadding = (4) ding(0, (8), 0, (8)) val colors = or(context, _invaild_color) tColor(colors) ze = 16f return btn } /** * 创建CheckBox */ fun createPassengerCheckBox(context: Context): CheckBox { val box = CheckBox(context) val params = Params( _PARENT, _CONTENT) Params = params ding(0, (12), 0, (12)) return box }}

class MainActivity : AppCompatActivity() { private var mSelectNuts: ArraySet = ArraySet() private var mSelectNumber: Int = 0 private val selectColor = _color_primary private val normalColor = _invaild_color private var fruitList: MutableList = mutableListOf() private var nutList: MutableList = mutableListOf() override fun onCreate(savedInstanceState: Bundle?) { te(savedInstanceState) setContentView(ty_main) initView() initData() } private fun initView() { group_fruit_heckedChangeListener { group, checkedId -> val btn = ewById(checkedId) if (btn != null) { val colors = orStateList(selectColor) tColor(colors) tColor(colors) val tag = as Int mSelectNumber = tag } } lickListener { val selectFruit = fruitList[mSelectNumber].content val count = layout_nut_ount () for (i in 0 until count) { val child = layout_nut_ldAt(i) as CheckBox if (ked) { (nutList[i].content) } } tv_ = "下单的商品:${selectFruit},${String(",")}" } } private fun initData() { { add(FruitBean("苹果", "1", false)) add(FruitBean("西⽠", "1", false)) add(FruitBean("草莓", "2", false)) add(FruitBean("车厘⼦", "3", false)) } { add(FruitBean("核桃", "1", false)) add(FruitBean("杏仁", "1", false)) add(FruitBean("松⼦", "2", false)) } initFruitInfo(fruitList, group_fruit_info) { val id = teViewId() TripRadioButton(this, id) } initNutInfo(nutList, layout_nut_select) { PassengerCheckBox(this) } } private fun initFruitInfo(list: List, parent: ViewGroup, createView: () -> TextView) { hWithIndex { index, bean -> val view = createView() val baseContent = t var content = "" if ( == "1") { led = true lorResource = selectColor content = baseContent } else if ( == "2") { content = "$baseContent(没有库存,不可选择)" led = false lorResource = _color_gray } else { content = "$baseContent(抢光了)" led = false lorResource = _color_gray } = content = index w(view) } } private fun initNutInfo(nutList: MutableList, parent: LinearLayout, createView: () -> CheckBox) { hWithIndex { index, bean -> val view = createView() = t lorResource = selectColor DrawableResource = or_rect_checkbox heckedChangeListener { buttonView, isChecked -> if (isChecked) { lorResource = selectColor } else { lorResource = normalColor } } w(view) } }}布局⽂件

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信