未验证 提交 73e84741 编写于 作者: S Shayan Sabahi 提交者: GitHub

Resolves #1961, allow choice list control color to be changed programatically (#1972)

* Change RadioButton's colors programmatically

* Update sample to demonstrate changing RadioButton's colors programmatically
上级 be74a35b
......@@ -19,6 +19,7 @@ import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.appcompat.widget.AppCompatRadioButton
import androidx.core.widget.CompoundButtonCompat
import androidx.recyclerview.widget.RecyclerView
......@@ -72,7 +73,9 @@ internal class SingleChoiceDialogAdapter(
disabledItems: IntArray?,
initialSelection: Int,
private val waitForActionButton: Boolean,
internal var selection: SingleChoiceListener
internal var selection: SingleChoiceListener,
@ColorInt private val checkedColor: Int,
@ColorInt private val uncheckedColor: Int
) : RecyclerView.Adapter<SingleChoiceViewHolder>(),
DialogAdapter<CharSequence, SingleChoiceListener> {
......@@ -117,7 +120,10 @@ internal class SingleChoiceDialogAdapter(
.let {
CompoundButtonCompat.setButtonTintList(
viewHolder.controlView,
createColorSelector(dialog.windowContext, checked = it[0], unchecked = it[1])
createColorSelector(dialog.windowContext,
checked = if (checkedColor == -1) it[0] else checkedColor,
unchecked = if (uncheckedColor == -1) it[1] else uncheckedColor
)
)
}
......
......@@ -20,6 +20,7 @@ package com.afollestad.materialdialogs.list
import android.util.Log
import androidx.annotation.ArrayRes
import androidx.annotation.CheckResult
import androidx.annotation.ColorInt
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.WhichButton.POSITIVE
import com.afollestad.materialdialogs.actions.setActionButtonEnabled
......@@ -42,6 +43,8 @@ import com.afollestad.materialdialogs.utils.MDUtil.getStringArray
disabledIndices: IntArray? = null,
initialSelection: Int = -1,
waitForPositiveButton: Boolean = true,
@ColorInt checkedColor: Int = -1,
@ColorInt uncheckedColor: Int = -1,
selection: SingleChoiceListener = null
): MaterialDialog {
assertOneSet("listItemsSingleChoice", items, res)
......@@ -72,7 +75,9 @@ import com.afollestad.materialdialogs.utils.MDUtil.getStringArray
disabledItems = disabledIndices,
initialSelection = initialSelection,
waitForActionButton = waitForPositiveButton,
selection = selection
selection = selection,
checkedColor = checkedColor,
uncheckedColor = uncheckedColor
)
)
}
......
......@@ -333,6 +333,20 @@ class MainActivity : AppCompatActivity() {
}
}
R.id.single_choice_custom_radio_color.onClickDebounced {
MaterialDialog(this).show {
title(R.string.socialNetworks)
listItemsSingleChoice(
R.array.socialNetworks, initialSelection = 1, checkedColor = Color.RED, uncheckedColor = Color.CYAN
) { _, index, text ->
toast("Selected item $text at index $index")
}
positiveButton(R.string.choose)
debugMode(debugMode)
lifecycleOwner(this@MainActivity)
}
}
R.id.multiple_choice.onClickDebounced {
MaterialDialog(this).show {
title(R.string.socialNetworks)
......
......@@ -168,6 +168,12 @@
style="@style/SampleButton"
/>
<Button
android:id="@+id/single_choice_custom_radio_color"
android:text="Single Choice, Custom Radio Color"
style="@style/SampleButton"
/>
<!-- Multiple Choice Lists -->
<TextView
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册