Ring是一种通用的编程语言,可以用来开发桌面、Web和移动应用。Ring语言是免费开源的,并且支持多种编程范式,包括命令式、过程式、面向对象、函数式、声明式和自然式。本文将介绍如何使用Ring语言开发一个用于存储体重信息(日期、时间和体重)的Android应用。这个应用是使用RingQt在Qt框架基础上开发Android应用的一个示例。值得注意的是,将编写的代码同样适用于桌面(Windows、Linux和MacOS X)和其他移动平台。
可以下载Ring语言,也可以查看关于该语言的其他文章:
首先,将创建一个新的文件来存储应用程序代码。文件名将被命名为weighthistory.ring。在应用程序的开始,将加载guilib.ring库,这个库提供了可以用于创建应用程序用户界面的GUI类。
Load "guilib.ring"
MyApp = new qApp {
$ApplicationObject = "oApp" # 用于调用事件时使用
oApp = new App
exec()
oApp.CloseDatabase()
}
接下来,将开始定义类,即App类。在类名之后,在定义类属性的类区域,将有:
还有其他用户界面属性,如layoutButtons、label1、text1、btnAdd、btnDelete、LayoutData、Table1、LayoutClose和LayoutMain。然后将调用OpenDatabase()方法和ShowRecords()方法。
class App
cDir = currentdir() + "/"
oCon
aIDs = []
win1 = new QWidget() {
setWindowTitle("Weight History")
resize(600, 600)
layoutButtons = new QHBoxLayout() {
label1 = new QLabel(win1) { setText("Weight") }
text1 = new QLineEdit(win1)
btnAdd = new QPushButton(win1) {
setText("Add")
setClickEvent($ApplicationObject + ".AddWeight()")
}
btnDelete = new QPushButton(win1) {
setText("Delete")
setClickEvent($ApplicationObject + ".DeleteWeight()")
}
addWidget(label1)
addWidget(text1)
addWidget(btnAdd)
addWidget(btnDelete)
}
layoutData = new QHBoxLayout() {
Table1 = new QTableWidget(win1) {
setRowCount(0)
setColumnCount(3)
setSelectionBehavior(QAbstractItemView_SelectRows)
setHorizontalHeaderItem(0, new QTableWidgetItem("Date"))
setHorizontalHeaderItem(1, new QTableWidgetItem("Time"))
setHorizontalHeaderItem(2, new QTableWidgetItem("Weight"))
setItemChangedEvent($ApplicationObject + ".ItemChanged()")
setAlternatingRowColors(true)
horizontalHeader().setStyleSheet("color: blue")
verticalHeader().setStyleSheet("color: red")
}
addWidget(Table1)
}
layoutClose = new QHBoxLayout() {
btnClose = new QPushButton(win1) { setText("Close") setClickEvent("MyApp.Quit()") }
addWidget(btnClose)
}
layoutMain = new QVBoxLayout() {
addLayout(layoutButtons)
addLayout(layoutData)
addLayout(layoutClose)
}
setLayout(layoutMain)
self.OpenDatabase()
self.ShowRecords()
show()
}
然后有openDatabase()方法,该方法检查SQLite数据库文件(weighthistory.db),如果找到则打开,如果未找到则使用创建表命令创建。
Func OpenDatabase
lCreate = False
if not fexists(cDir + "weighthistory.db")
lCreate = True
ok
new QSqlDatabase() {
this.oCon = addDatabase("QSQLITE") {
setDatabaseName("weighthistory.db")
Open()
}
}
if lCreate
new QSqlQuery() {
exec("create table weighthistory (id integer primary key, f_date varchar(10), f_time varchar(8), f_weight varchar(8) );")
delete()
}
ok
然后有CloseDatabase()方法,该方法使用连接对象(oCon)并调用Close()方法。
Func CloseDatabase
oCon.Close()
接下来是AddWeight()和DeleteWeight()方法。AddWeight()方法从文本框获取文本,然后将其传递给AddRecord()方法。DeleteWeight()方法检查表中的当前记录,然后执行SQL命令(Delete),在从aIDs列表获取记录ID后。
Func AddWeight
cWeight = text1.text()
AddRecord(cWeight)
Func DeleteWeight
Table1 {
nRow = CurrentRow()
if nRow >= 0
nID = this.aIDs[nRow+1]
new QSqlQuery() {
exec("delete from weighthistory where id = " + nID)
}
Del(this.aIDs, nRow+1)
removeRow(nRow)
selectRow(nRow)
ok
}
接下来的代码展示了AddRecord()和ShowRecords()方法。AddRecord()方法将体重作为参数获取,然后执行SQL命令(Insert Into)。ShowRecords()方法将执行SQL命令("select * from weighthistory")以获取记录数据,然后在表中显示数据。
Func AddRecord cWeight
new QSqlQuery() {
cStr = "insert into weighthistory (f_date,f_time,f_weight) values ('%f1','%f2','%f3')"
cDate = Date()
cTime = Time()
cStr = substr(cStr, "%f1", cDate)
cStr = substr(cStr, "%f2", cTime)
cStr = substr(cStr, "%f3", cWeight)
exec(cStr)
delete()
}
ShowRecords()
Table1.selectRow(table1.rowCount()-1)
Func ShowRecords
table1.setItemChangedEvent("")
aIDs = []
query = new QSqlQuery() {
exec("select * from weighthistory")
nRows = 0
this.Table1.setRowCount(0)
while moveNext()
this.table1 {
insertRow(nRows)
this.aIDs + query.value(0).toString()
for x = 1 to 3
cStr = query.value(x).toString()
item = new QTableWidgetItem(cStr)
setItem(nRows, x-1, item)
next
}
nRows++
end
delete()
}
table1.setItemChangedEvent($ApplicationObject + ".ItemChanged()")
最后有itemChanged()方法。使用此方法,在通过用户界面更新表控件中的数据后更新数据库中的表。该方法执行SQL命令(Update)。
Func ItemChanged
nRow = table1.currentRow()
if nRow >= 0
myitem = Table1.item(table1.currentRow(), 0)
cDate = myitem.text()
myitem = Table1.item(table1.currentRow(), 1)
cTime = myitem.text()
myitem = Table1.item(table1.currentRow(), 2)
cWeight = myitem.text()
new QSqlQuery() {
cStr = "update weighthistory set f_date ='%f1' , f_time = '%f2' , f_weight ='%f3' where id = " + this.aIDs[nRow+1]
cStr = substr(cStr, "%f1", cDate)
cStr = substr(cStr, "%f2", cTime)
cStr = substr(cStr, "%f3", cWeight)
exec(cStr)
delete()
}
ok