forked from nstudio/nativescript-checkbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckbox.android.ts
More file actions
148 lines (123 loc) · 4.98 KB
/
checkbox.android.ts
File metadata and controls
148 lines (123 loc) · 4.98 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { CheckBoxInterface } from "./";
import { View, Property, booleanConverter } from "ui/core/view";
import { Color } from "color";
import { isAndroid, device } from "platform";
import { Font } from "ui/styling/font";
import enums = require("ui/enums");
import style = require("ui/styling/style");
import app = require("application");
import { backgroundColorProperty, colorProperty, fontInternalProperty } from "ui/core/view";
declare var android: any;
const textProperty = new Property<CheckBox, string>({
name: "text"
});
const fillColorProperty = new Property<CheckBox, string>({
name: "fillColor"
});
const tintColorProperty = new Property<CheckBox, string>({
name: "tintColor"
});
const checkedProperty = new Property<CheckBox, boolean>({
name: "checked",
valueConverter: booleanConverter
});
export class CheckBox extends View implements CheckBoxInterface {
public checkStyle: string;
public checkPadding: string;
public checkPaddingLeft: string;
public checkPaddingTop: string;
public checkPaddingRight: string;
public checkPaddingBottom: string;
constructor() {
super();
}
[fillColorProperty.setNative](value: string) {
this.setButtonTintList(value);
}
[tintColorProperty.setNative](value: string) {
this.setButtonTintList(value);
}
private setButtonTintList(value: string) {
if (device.sdkVersion >= "21") {
this.nativeView.setButtonTintList(android.content.res.ColorStateList.valueOf(new Color(value).android));
}
}
[backgroundColorProperty.setNative](value: Color) {
this.nativeView.setTextColor(value.android);
}
[colorProperty.setNative](value: Color) {
this.nativeView.setTextColor(value.android);
}
[fontInternalProperty.setNative](value: Font) {
if (value) {
var typeface = value.getAndroidTypeface();
this.nativeView.setTypeface(typeface);
this.nativeView.setTextSize(value.fontSize);
}
}
[textProperty.setNative](value: string) {
this.nativeView.setText(value);
}
[checkedProperty.setNative](value: boolean) {
this.nativeView.setChecked(value);
}
public createNativeView() {
let _nativeView = new android.support.v7.widget.AppCompatCheckBox(this._context, null);
if (this.checkPaddingLeft) {
_nativeView.setPadding(parseInt(this.checkPaddingLeft), _nativeView.getPaddingTop(), _nativeView.getPaddingRight(), _nativeView.getPaddingBottom());
}
if (this.checkPaddingTop) {
_nativeView.setPadding(_nativeView.getPaddingLeft(), parseInt(this.checkPaddingTop), _nativeView.getPaddingRight(), _nativeView.getPaddingBottom());
}
if (this.checkPaddingRight) {
_nativeView.setPadding(_nativeView.getPaddingLeft(), _nativeView.getPaddingTop(), parseInt(this.checkPaddingRight), _nativeView.getPaddingBottom());
}
if (this.checkPaddingBottom) {
_nativeView.setPadding(_nativeView.getPaddingLeft(), _nativeView.getPaddingTop(), _nativeView.getPaddingRight(), parseInt(this.checkPaddingBottom));
}
if (this.checkPadding) {
let pads = this.checkPadding.toString().split(',');
switch (pads.length) {
case 1:
_nativeView.setPadding(parseInt(pads[0]), parseInt(pads[0]), parseInt(pads[0]), parseInt(pads[0]));
break;
case 2:
_nativeView.setPadding(parseInt(pads[0]), parseInt(pads[1]), parseInt(pads[0]), parseInt(pads[1]));
break;
case 3:
_nativeView.setPadding(parseInt(pads[0]), parseInt(pads[1]), parseInt(pads[2]), parseInt(pads[1]));
break;
case 4:
_nativeView.setPadding(parseInt(pads[0]), parseInt(pads[1]), parseInt(pads[2]), parseInt(pads[3]));
break;
}
}
/// works with class styling - Brad
if (!this.style.fontSize) {
this.style.fontSize = 15;
}
if (this.checkStyle) {
const drawable = app.android.context.getResources().getIdentifier(this.checkStyle, "drawable", app.android.context.getPackageName());
_nativeView.setButtonDrawable(drawable);
}
var that = new WeakRef(this);
_nativeView.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener({
get owner() {
return that.get();
},
onCheckedChanged: function (sender, isChecked) {
if (this.owner) {
checkedProperty.nativeValueChange(this.owner, isChecked);
}
}
}));
return _nativeView;
}
public toggle(): void {
this.nativeView.toggle();
}
}
textProperty.register(CheckBox);
fillColorProperty.register(CheckBox);
tintColorProperty.register(CheckBox);
checkedProperty.register(CheckBox);