//设置对话框内容样式
class TextAttributeModel {
fontSize: number | string | Resource = 16
fontColor: ResourceColor = "#000000"
fontWeight: number | FontWeight | string = 400
maxLines: number = -1
backgroundColor: ResourceColor = ''
}
getMessageAttribute(): TextAttributeModel {
let messageAttribute = new TextAttributeModel()
messageAttribute.fontColor = "#666666"
messageAttribute.fontSize = 16
return messageAttribute
}
//设置对话框标题样式
class TextAttributeModel {
fontSize: number | string | Resource = 16
fontColor: ResourceColor = "#000000"
fontWeight: number | FontWeight | string = 400
maxLines: number = -1
backgroundColor: ResourceColor = ''
}
getTitleAttribute(): TextAttributeModel {
let titleAttribute = new TextAttributeModel()
titleAttribute.fontSize = 20
return titleAttribute
}
```
###### 对话框搭配单选项使用
```
//监听选择的选项
class SingleChoiceListener1 implements SingleChoiceListener {
onSelected(value: string, index: number) {
prompt.showToast({ message: "Selected item " + value + " at index " + index })
}
}
function singleChoiceListener(): SingleChoiceListener {
let back: SingleChoiceListener = new SingleChoiceListener1()
return back
}
```
//监听选择回调
class ColorCallback1 implements ColorCallback {
onSelected(color: string) {
console.info('onSelected color is ' + color)
prompt.showToast({ message: "Selected color: " + color })
}
}
function colorCallback(): ColorCallback {
let back: ColorCallback = new ColorCallback1()
return back
}}
//确定/取消监听回调
class ClickCallback1 implements ClickCallback {
value1: string = ''
type: number = 0
onClick(value?: string) {
if(this.type === 0) {
console.info(this.value1)
}else if(this.type === 1) {
console.info('ClickCallback when the confirm button is clicked')
prompt.showToast({ message: this.value1 })
}else if(this.type === 2) {
prompt.showToast({ message: this.value1 })
}else {
console.info('ClickCallback when the confirm button is clicked')
prompt.showToast({ message: this.value1 + value })
}
}
constructor(value: string, type: number){
this.value1 = value
this.type = type
}
}
function Callback(value1: string, type: number): ClickCallback {
let back: ClickCallback = new ClickCallback1(value1, type)
return back
}
function Callback(value1: string, type: number): ClickCallback {
let back: ClickCallback = new ClickCallback1(value1, type)
return back
}
//初始化对话框
init()
//打开对话框
private showColorPrimaryDialog() {
this.model.reset()
this.model.title($r('xxxx'), this.getTitleAttribute())
this.model.colorChooser(ColorPalette.primary, ColorPalette.primarySub, -1, true, false, false, false, colorCallback())
this.model.positiveButton("xxxx", Callback('ClickCallback when the confirm button is clicked', 0))
this.model.negativeButton("xxxx", Callback('ClickCallback when the negative button is clicked', 0))
this.model.setScrollHeight(400)
this.model.setStacked(false)
this.dialogController.open()
}