-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathEasyDimensionActivity.kt
129 lines (115 loc) · 3.92 KB
/
EasyDimensionActivity.kt
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.haoge.sample.easyandroid.activities
import android.util.TypedValue
import android.widget.EditText
import android.widget.RadioGroup
import android.widget.TextView
import butterknife.OnClick
import com.haoge.easyandroid.easy.EasyDimension
import com.haoge.easyandroid.easy.EasyToast
import com.haoge.sample.easyandroid.BaseActivity
import com.haoge.sample.easyandroid.R
/**
* @author haoge on 2018/5/10
*/
open class EasyDimensionActivity: BaseActivity() {
override fun getLayoutId(): Int {
return R.layout.activity_dimen_converter
}
val mInput:EditText by lazy { findViewById<EditText>(R.id.number_input) }
val mShown:TextView by lazy { findViewById<TextView>(R.id.shown_tv) }
val mSelector:RadioGroup by lazy { findViewById<RadioGroup>(R.id.selector) }
var mEasyDimension: EasyDimension? = null
var value:Float = 0.0f
var unitName:String = ""
@OnClick(R.id.create)
fun create() {
value = mInput.text.toString().safeToInt().toFloat()
val (unit:Int, name:String) = when(mSelector.checkedRadioButtonId) {
R.id.select_PX -> Pair(TypedValue.COMPLEX_UNIT_PX, "PX")
R.id.select_DIP -> Pair(TypedValue.COMPLEX_UNIT_DIP, "DIP")
R.id.select_SP -> Pair(TypedValue.COMPLEX_UNIT_SP, "SP")
R.id.select_PT -> Pair(TypedValue.COMPLEX_UNIT_PT, "PT")
R.id.select_IN -> Pair(TypedValue.COMPLEX_UNIT_IN, "IN")
R.id.select_MM -> Pair(TypedValue.COMPLEX_UNIT_MM, "MM")
else -> Pair(TypedValue.COMPLEX_UNIT_PX, "PX")
}
unitName = name
mEasyDimension = EasyDimension.create(value, unit)
mShown.text = "转换器创建成功,数值为${value}单位为$unitName"
}
@OnClick(R.id.toPX)
fun toPX() {
if (checkConverterCreated()) {
val result = """
>原始数值为${value}单位为$unitName
>转换后数值为${mEasyDimension?.toPX()}单位为PX
""".trimMargin(">")
mShown.text = result
}
}
@OnClick(R.id.toSP)
fun toSP() {
if (checkConverterCreated()) {
val result = """
>原始数值为${value}单位为$unitName
>转换后数值为${mEasyDimension?.toSP()}单位为SP
""".trimMargin(">")
mShown.text = result
}
}
@OnClick(R.id.toDIP)
fun toDIP() {
if (checkConverterCreated()) {
val result = """
>原始数值为${value}单位为$unitName
>转换后数值为${mEasyDimension?.toDIP()}单位为DIP
""".trimMargin(">")
mShown.text = result
}
}
@OnClick(R.id.toPT)
fun toPT() {
if (checkConverterCreated()) {
val result = """
>原始数值为${value}单位为$unitName
>转换后数值为${mEasyDimension?.toPT()}单位为PT
""".trimMargin(">")
mShown.text = result
}
}
@OnClick(R.id.toIN)
fun toIN() {
if (checkConverterCreated()) {
val result = """
>原始数值为${value}单位为$unitName
>转换后数值为${mEasyDimension?.toIN()}单位为IN
""".trimMargin(">")
mShown.text = result
}
}
@OnClick(R.id.toMM)
fun toMM() {
if (checkConverterCreated()) {
val result = """
>原始数值为${value}单位为$unitName
>转换后数值为${mEasyDimension?.toMM()}单位为MM
""".trimMargin(">")
mShown.text = result
}
}
fun checkConverterCreated():Boolean {
return if (mEasyDimension == null) {
EasyToast.DEFAULT.show("请先创建转换器")
false
} else {
true
}
}
}
fun String.safeToInt():Int {
return try {
this.toInt()
} catch (e:Exception) {
0
}
}