blob: 0f496385677ff056f16105a1e4a026ab32af75df (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package net.lacolaco.smileessence.view
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.widget.Button
import net.lacolaco.smileessence.R
class ThreeStateButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = android.R.attr.buttonStyle, defStyleRes: Int = 0) : Button(context, attrs, defStyleAttr, defStyleRes) {
var state = STATE_OFF
set(s) {
field = s
text = texts[this.state]
background = backgrounds[this.state]
isEnabled = this.state != STATE_LOCKED
}
private val texts = arrayOfNulls<String>(3)
private val backgrounds = arrayOfNulls<Drawable>(3)
init {
val ta = context.obtainStyledAttributes(attrs, R.styleable.ThreeStateButton)
texts[STATE_OFF] = ta.getString(R.styleable.ThreeStateButton_off_text)
texts[STATE_ON] = ta.getString(R.styleable.ThreeStateButton_on_text)
texts[STATE_LOCKED] = ta.getString(R.styleable.ThreeStateButton_locked_text)
backgrounds[STATE_OFF] = ta.getDrawable(R.styleable.ThreeStateButton_off_background)
backgrounds[STATE_ON] = ta.getDrawable(R.styleable.ThreeStateButton_on_background)
backgrounds[STATE_LOCKED] = ta.getDrawable(R.styleable.ThreeStateButton_locked_background)
ta.recycle()
state = STATE_OFF
}
companion object {
val STATE_OFF = 0
val STATE_ON = 1
val STATE_LOCKED = 2
}
}
|