HarmonyOS 鸿蒙Next material-dialogs三方框架获取值
HarmonyOS 鸿蒙Next material-dialogs三方框架获取值 使用material-dialogs弹窗,onSelected中获取选择后的值怎么传给组件中的sex?
使用material-dialogs弹窗,选择了一个item,获取到选择的值在组件外的类中方法onSelected中,怎么把选择的值传给组件中的sex?
楼主你好:
import { ClickCallback, DialogAttributeModel, MaterialDialog, SingleChoiceListener } from '@ohos/material-dialogs';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
model: MaterialDialog.Model = new MaterialDialog.Model();
dialogAttribute = new DialogAttributeModel()
@State items: string[] = ['男', '女']
@State listenser: SingleChoiceListener1 = new SingleChoiceListener1()
existDialog() {
this.dialogController.close()
}
dialogController: CustomDialogController = new CustomDialogController({
builder: MaterialDialog({
model: this.model, dialogAttribute: this.dialogAttribute
}),
cancel: this.existDialog,
autoCancel: true,
alignment: DialogAlignment.Center,
customStyle: true
})
build() {
Row() {
Column() {
Text(this.listenser.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
//1. 弹出dialog
this.model.reset()
this.model.listItemsSingleChoice(this.items, 2, true, null, -1, -1, this.listenser)
this.model.positiveButton('确定', Callback('', 0))
this.model.negativeButton('取消', Callback('', 1))
this.model.setStacked(false)
this.dialogController.open()
})
}
.width('100%')
}
.height('100%')
}
}
class SingleChoiceListener1 implements SingleChoiceListener {
@Track message: string | Resource | undefined;
onSelected(value: string, index: number) {
//2. 获取到选择的性别(value = 男/女)
//3. 需要把value值传给message属性,然后刷新界面,显示选择的性别
this.message = value;
}
}
function Callback(value1: string, type: number): ClickCallback {
let back: ClickCallback = new ClickCallback1(value1, type)
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')
}
}
constructor(value: string, type: number) {
this.value1 = value
this.type = type
}
}
更多关于HarmonyOS 鸿蒙Next material-dialogs三方框架获取值的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS(鸿蒙)系统中,使用第三方框架如material-dialogs获取用户输入的值,通常涉及到框架的特定API调用。由于material-dialogs是为Android设计的,而HarmonyOS虽然借鉴了Android的部分架构,但在API层面存在差异。因此,直接在HarmonyOS中使用Android的第三方库可能会遇到问题。
不过,假设有一个适用于HarmonyOS的material-dialogs的移植版本或者类似功能的库,获取值的基本步骤可能如下:
-
引入库:确保你的HarmonyOS项目已经正确引入了material-dialogs或其替代库的依赖。
-
创建对话框:使用库提供的API创建一个对话框实例,并设置所需的输入字段。
-
设置回调:为对话框的确认按钮设置回调,以便在用户点击确认时获取输入值。
-
处理回调:在回调中,通过对话框实例提供的方法获取用户输入的值。
示例代码(伪代码,因为具体API可能不同):
// 假设有一个适用于HarmonyOS的MaterialDialog实例
MaterialDialog dialog = new MaterialDialog.Builder(context)
.title("输入")
.input("请输入内容", "", new MaterialDialog.InputCallback() {
@Override
public void onInput(MaterialDialog dialog, CharSequence input) {
// 获取用户输入的值
String userInput = input.toString();
}
})
.positiveText("确认")
.show();
请注意,上述代码是基于Android的material-dialogs库编写的伪代码,实际使用时需要根据HarmonyOS兼容库的API进行调整。
如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html