主按钮 执行 Telegram 迷你应用程序 主按钮。 初始化要初始化组件,请使用 initMainButton 函数: - import { initMainButton } from '@telegram-apps/sdk';; H; P' T) y: _: M& Q! g
- - n1 B# A3 w" K, |8 M- I
- const [mainButton] = initMainButton();
复制代码 按钮可见性要控制主按钮的可见性,开发人员可以使用 show() 和 hide() 等方法。 它们都会更新组件的 isVisible 属性: - mainButton.show();$ `$ x2 J% Q/ y" n" |) A$ c
- console.log(mainButton.isVisible); // true : L2 ], i4 q8 g
- 3 e$ n% U1 `& T$ D) K7 z
- mainButton.hide();
5 J+ }4 c6 M, Z, o' e- _ - console.log(mainButton.isVisible); // false
复制代码 装载机主按钮内部可以显示一个加载器。 要控制其可见性, 使用 showLoader() 和 hideLoader() 方法。 isLoaderVisible 属性将被更改。 - mainButton.showLoader();
( C5 S7 |1 J& Q' X% Q6 R6 _6 V - console.log(mainButton.isLoaderVisible); // true & L& w, }" i; i2 @& b
i0 o# b; u1 C0 o5 B. m2 }- mainButton.hideLoader();
( q% w) X j2 \6 z - console.log(mainButton.isLoaderVisible); // false
复制代码 活跃状态可以通过调用 disable() 和 enable() 方法启用或禁用主按钮。 这两个方法都会更新 isEnabled 属性。 - mainButton.enable();4 D5 @2 p" u2 [, a
- console.log(mainButton.isEnabled); // true
, q0 ^4 r3 F$ S' _ \ - 4 s$ t1 y" ^6 T' n; y
- mainButton.disable();
/ s1 ?- l$ t* ?7 j$ Q/ q5 Z. L - console.log(mainButton.isEnabled); // false
复制代码 启用主按钮后,用户就可以点击它。 因此,主按钮将 接收click事件。背景颜色要更新主按钮的背景颜色,请使用 setBackgroundColor(color: RGB) 方法。 将更新 backgroundColor 属性。 - mainButton.setBackgroundColor('#ffffaa');
" _7 P8 u# |, r* Z - console.log(mainButton.color); // '#ffffaa'
复制代码 文字颜色要更新主按钮文本的颜色,请使用 setTextColor(color: RGB) 方法。 它将更新 textColor属性。 - mainButton.setTextColor('#cca233');+ y6 D* C" Y7 J" u" A
- console.log(mainButton.textColor); // '#cca233'
复制代码 文本要更新主按钮文本,请使用 setText(text: string) 方法。 它将更新 text 属性。 - mainButton.setText('Submit');
3 F! `3 }9 L# g' P/ h7 ? X - console.log(mainButton.text); // 'Submit'
复制代码 设置多个属性有时,连续设置几个主按钮参数可能会导致 UI 出现问题。 为了避免这个问题,可以使用 setParams 方法: - mainButton.setParams({- S" N( Q# b! q. w8 u/ t* M
- backgroundColor: '#aa1388',& V% h: n. W" I& a* W4 [1 D
- text: 'Stop',
+ i) b9 I0 ~7 X) V1 c - isVisible: true,
- m: P7 x/ H ]' j( W - });
复制代码 事件可被 跟踪 的事件列表: [td]事件 | 监听器 | 触发条件 | click | () => void | 点击了主按钮 | change | () => void | 组件中的某些部分发生了变化 | change:backgroundColor | (value: RGB) => void | 更改了 backgroundColor 属性 | change:isLoaderVisible | (value: boolean) => void | 更改了 isLoaderVisible 属性 | change:isEnabled | (value: boolean) => void | 已更改 isEnabled 属性 | change:isVisible | (value: boolean) => void | 更改了 isVisible 属性 | change:text | (value: string) => void | 更改了 text 属性 | change:textColor | (value: RGB) => void | 更改了 textColor 属性 |
; |( ?+ ^( ~( y5 x% w1 X3 c( X |