my.setScreenBrightness
setScreenBrightness
my.setScreenBrightness
là API dùng để điều chỉnh độ sáng màn hình.
Khả dụng: Hỗ trợ từ version 1.84.19 trở lên.
Quét mã để trải nghiệm
Xem code mẫu trên Tini StudioDemo
Trải nghiệm thử với trình giả lập bên dưới
API Params
Thuộc tính | Kiểu dữ liệu | Bắt buộc | Mô tả |
---|---|---|---|
brightness | number | ✓ | Độ sáng màn hình. Nhận giá trị trong khoảng từ 0 - 1. |
success | Function | Callback function khi điều chỉnh độ sáng thành công. | |
fail | Function | Callback function khi điều chỉnh độ sáng bất thành. | |
complete | Function | Callback function khi điều chỉnh độ sáng hoàn tất bất kể thành công hay thất bại. |
Sample Code
<view class="page">
<block-header title="Usage" description="Get battery information" />
<view class="block-content">
<view>Adjust screen brightness</view>
<view class="page-section-demo">
<slider value="{{brightness*100}}" onChange="sliderChange" />
</view>
</view>
<view class="block-content">
<view class="page-section-demo">
<button type="primary" onTap="onGetBrightness">Get screen brightness</button>
</view>
</view>
</view>
Page({
data: {
status: false,
brightness: 0
},
onLoad() {
my.getScreenBrightness({
success: (res) =>
this.setData({
brightness: res.brightness
})
});
},
sliderChange(e) {
const _value = e.detail.value / 100;
my.setScreenBrightness({
brightness: _value,
success: (res) => {
this.setData({
brightness: _value
});
}
});
},
onGetBrightness() {
my.getScreenBrightness({
success: (res) => {
my.alert({
title: 'Success',
content: 'Độ sáng màn hình hiện tại:' + res.brightness
});
},
fail: (res) => {
my.alert({ title: 'Fail', content: JSON.stringify(res) });
}
});
}
});