在golang中如何使用rating-input來進行打分及評價

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

在我們之前的C++文章“利用rating-input PreviewWidget來對事物進行評價及打分”,我們已經展示了如何使用C++來在Scope中的Preview中對事物進行評價或打分。在今天的這篇文章中,我們將介紹如何在Go Scope中來做同樣的事。我們可以通過這個例子來展示如何捕獲在Go Preview中的按鈕並得到它們的action id以進行分別的處理。


在Go檔案中的Preview方法中:


unc (s *MyScope) Preview(result *scopes.Result, metadata *scopes.ActionMetadata, reply *scopes.PreviewReply, cancelled <-chan bool) error {layout1col := scopes.NewColumnLayout(1)layout2col := scopes.NewColumnLayout(2)layout3col := scopes.NewColumnLayout(3)// Single column layoutlayout1col.AddColumn("image", "header", "summary", "rating", "actions")// Two column layoutlayout2col.AddColumn("image")layout2col.AddColumn("header", "summary", "rating", "actions")// Three cokumn layoutlayout3col.AddColumn("image")layout3col.AddColumn("header", "summary", "rating", "actions")layout3col.AddColumn()// Register the layouts we just createdreply.RegisterLayout(layout1col, layout2col, layout3col)header := scopes.NewPreviewWidget("header", "header")// It has title and a subtitle propertiesheader.AddAttributeMapping("title", "title")header.AddAttributeMapping("subtitle", "subtitle")// Define the image sectionimage := scopes.NewPreviewWidget("image", "image")// It has a single source property, mapped to the result's art propertyimage.AddAttributeMapping("source", "art")// Define the summary sectiondescription := scopes.NewPreviewWidget("summary", "text")// It has a text property, mapped to the result's description propertydescription.AddAttributeMapping("text", "description")actions := scopes.NewPreviewWidget("actions", "actions")actions.AddAttributeValue("actions", []actionInfo{actionInfo{Id: "my_action", Label: "Close"},actionInfo{Id: "my_action2", Label: "Refresh"},})rating := scopes.NewPreviewWidget("rating", "rating-input")rating.AddAttributeValue("visible", "both")rating.AddAttributeValue("required", "rating")var scope_data stringmetadata.ScopeData(scope_data)if len(scope_data) > 0 {extra := scopes.NewPreviewWidget("extra", "text")extra.AddAttributeValue("text", "test Text")reply.PushWidgets(header, image, description, actions, rating, extra)} else {reply.PushWidgets(header, image, description, rating, actions)}return nil}


我們可以在上面的文字中,看到:


rating := scopes.NewPreviewWidget("rating", "rating-input")rating.AddAttributeValue("visible", "both")rating.AddAttributeValue("required", "rating")


它用來產生一個rating的PreviewWidget:即有rating,也有review,所以在設定“visible”中為both。


func (sc *MyScope) PerformAction(result *scopes.Result, metadata *scopes.ActionMetadata, widgetId, actionId string) (*scopes.ActivationResponse, error) {log.Printf("Perform action for widget=%s, action=%s\n", widgetId, actionId)// var scope_data interface{}var scope_data map[string]interface{}metadata.ScopeData(&scope_data)log.Println("rating: ", scope_data["rating"])log.Println("review: ", scope_data["review"])for key, value := range scope_data {log.Println("key: ", key)log.Println("value: ", value)}if widgetId == "actions" && actionId == "my_action" {resp := scopes.NewActivationResponse(scopes.ActivationHideDash)resp.SetScopeData([]string{"hello", "world"})return resp, nil} return scopes.NewActivationResponse(scopes.ActivationShowPreview), nil}


在Go 檔案中實現上面的PerformAction就可以捕獲在Preview中的按鈕事件。我們在上面的代碼中列印出rating及review的值。


運行我們的代碼:


  


在運行時的輸出:




整個項目的源碼在:git clone https://gitcafe.com/ubuntu/goscope_rating.git


我們可以在Terminal中:


$chmod +x *.sh


通過上述命令來使得.sh檔案變為可以執行的檔案


$./run.sh


通過上面的指令碼執行,在Desktop上運行我們的Scope


$./build.sh -d


通過我們上面的指令碼執行,可以編譯goscope,並部署到手機中去


$./clean.sh


通過上面的指令碼執行,清除所有的編譯的中間檔案



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.