最终效果:
核心代码:
object OrderViewCenter {
/**
* 创建RadioButton
*/
fun createTripRadioButton(context: Context, radioId: Int): RadioButton {
val btn = RadioButton(context)
btn.id = radioId
val params = RadioGroup.LayoutParams(
RadioGroup.LayoutParams.MATCH_PARENT,
RadioGroup.LayoutParams.WRAP_CONTENT)
btn.layoutParams = params
btn.buttonDrawable = null
var drawable: Drawable = context.resources.getDrawable(R.drawable.selector_radio)
drawable.setBounds(0, 0, context.dip(24), context.dip(24))
btn.setCompoundDrawables(drawable, null, null, null)
btn.compoundDrawablePadding = context.dip(4)
btn.setPadding(0, context.dip(8), 0, context.dip(8))
val colors = ContextCompat.getColor(context, R.color.text_invaild_color)
btn.setTextColor(colors)
btn.textSize = 16f
return btn
}
/**
* 创建CheckBox
*/
fun createPassengerCheckBox(context: Context): CheckBox {
val box = CheckBox(context)
val params = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
box.layoutParams = params
box.setPadding(0, context.dip(12), 0, context.dip(12))
return box
}
}
class MainActivity : AppCompatActivity() {
private var mSelectNuts: ArraySet<String> = ArraySet()
private var mSelectNumber: Int = 0
private val selectColor = R.color.text_color_primary
private val normalColor = R.color.text_invaild_color
private var fruitList: MutableList<FruitBean> = mutableListOf()
private var nutList: MutableList<FruitBean> = mutableListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initView()
initData()
}
private fun initView() {
group_fruit_info.setOnCheckedChangeListener { group, checkedId ->
val btn = group.findViewById<RadioButton>(checkedId)
if (btn != null) {
val colors = resources.getColorStateList(selectColor)
btn.setTextColor(colors)
val tag = btn.tag as Int
mSelectNumber = tag
}
}
btn.setOnClickListener {
val selectFruit = fruitList[mSelectNumber].content
val count = layout_nut_select.childCount
mSelectNuts.clear()
for (i in 0 until count) {
val child = layout_nut_select.getChildAt(i) as CheckBox
if (child.isChecked) {
mSelectNuts.add(nutList[i].content)
}
}
tv_result.text = "下单的商品:${selectFruit},${mSelectNuts.joinToString(",")}"
}
}
private fun initData() {
fruitList.apply {
add(FruitBean("苹果", "1", false))
add(FruitBean("西瓜", "1", false))
add(FruitBean("草莓", "2", false))
add(FruitBean("车厘子", "3", false))
}
nutList.apply {
add(FruitBean("核桃", "1", false))
add(FruitBean("杏仁", "1", false))
add(FruitBean("松子", "2", false))
}
initFruitInfo(fruitList, group_fruit_info) {
val id = View.generateViewId()
OrderViewCenter.createTripRadioButton(this, id)
}
initNutInfo(nutList, layout_nut_select) {
OrderViewCenter.createPassengerCheckBox(this)
}
}
private fun initFruitInfo(list: List<FruitBean>, parent: ViewGroup, createView: () -> TextView) {
list.forEachWithIndex { index, bean ->
val view = createView()
val baseContent = bean.content
var content = ""
if (bean.status == "1") {
view.isEnabled = true
view.textColorResource = selectColor
content = baseContent
} else if (bean.status == "2") {
content = "$baseContent(没有库存,不可选择)"
view.isEnabled = false
view.textColorResource = R.color.text_color_gray
} else {
content = "$baseContent(抢光了)"
view.isEnabled = false
view.textColorResource = R.color.text_color_gray
}
view.text = content
view.tag = index
parent.addView(view)
}
}
private fun initNutInfo(nutList: MutableList<FruitBean>, parent: LinearLayout, createView: () -> CheckBox) {
nutList.forEachWithIndex { index, bean ->
val view = createView()
view.text = bean.content
view.textColorResource = selectColor
view.buttonDrawableResource = R.drawable.selector_rect_checkbox
view.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
view.textColorResource = selectColor
} else {
view.textColorResource = normalColor
}
}
parent.addView(view)
}
}
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#fff">
<TextView
android:id="@+id/tv_select_action_trip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:text="单选"
android:textColor="#7A000000"
android:textSize="15sp" />
<RadioGroup
android:id="@+id/group_fruit_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:background="#fff"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:text="多选"
android:textColor="#7A000000"
android:textSize="15sp" />
<LinearLayout
android:id="@+id/layout_nut_select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"
android:visibility="visible"/>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#0f0"
android:text="下单"/>
<TextView
android:id="@+id/tv_result"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#3c3c3c"
android:text="下单的商品:"/>
</LinearLayout>
</LinearLayout>
源码地址:https://download.csdn.net/download/jingerlovexiaojie/18443237