Qt for Android 实现Toast消息提示框
这篇文章演示如何使用 QML 实现安卓 Toast 消息提示框功能,先看效果:
使用方法
首先在项目中添加 .qml 文件,在文件中插入 Toast 实现代码,如下代码:
import QtQuick 2.0 import QtQuick.Controls 1.4 import QtQuick.Layouts 1.2 import "." Rectangle { property bool isShow: false property alias text: t.text // 显示的文字 width: childrenRect.width height: childrenRect.height //z: 100 color: "#666666" opacity: isShow ? 1 : 0 //border.width: units.dp(1) //border.color: "white" radius: width/2 Behavior on opacity { NumberAnimation { duration: 1000 } } ColumnLayout { Label { //Layout.margins: t.height Layout.margins: units.dp(15) id: t; color: "white" text: "" } } Timer { id: toastTimer interval: 2000 onTriggered: isShow = false } // 显示toast函数 function showToast(text) { t.text = text; isShow = true; toastTimer.restart(); } }
Toast实现以后我们需要使用它,下面的代码是在主界面添加一个 Toast 控件,并定义它显示的位置:
Window { id: window .... Toast { id:toast anchors.horizontalCenter: parent.horizontalCenter anchors.bottom: parent.bottom anchors.bottomMargin: toast.height } }
在需要显示Toast消息提示框的时候只需要下面一句代码即可:
toast.showToast(qsTr("再按一次退出"))