使用第三方library,添加可互动的EditText用户可以选择手动输入hexCode或是直接选一个颜色
在app gradle中添加
implementation 'com.github.duanhong169:colorpicker:1.1.6'
这里贴出dialog的部分代码
colorPickerView.subscribe { color: Int, _: Boolean, _: Boolean ->
selectedColor = color
hexEditText.setText(ColorUtils.getHexStrFromInt(color))
hexEditText.setSelection(hexEditText.text?.length ?: 0)
indicatorCircle.background = ColorUtils.getCircleDrawable(selectedColor)
}
hexEditText.imeOptions = EditorInfo.IME_ACTION_DONE
colorPickerView.setInitialColor(selectedColor)
hexEditText.setOnEditorActionListener { v, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
v?.clearFocus()
v?.text = v?.text