This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
sample_code:new_file [2015/01/25 13:40] octazid created |
sample_code:new_file [2015/02/05 19:20] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
======New File Dialog====== | ======New File Dialog====== | ||
- | comming soon... | + | Sometimes you have to create new files in a folder.\\ |
+ | Its easy with if you use a dialog.\\ \\ | ||
+ | (look for the example to [[sample_code: | ||
+ | {{: | ||
+ | |||
+ | Save the **DlgNewFile.js** in the Droidscriptfolder where you need the file.\\ | ||
+ | You can call the dialog after you load the Script into your app at the OnStart() Event. | ||
+ | |||
+ | <code Javascript DlgNewFile.js> | ||
+ | / | ||
+ | * CreateNewFile Dialog | ||
+ | ===================================================================== | ||
+ | * Creation date: 20-01-2015 by octazid | ||
+ | * Last update: 22-01-2015 by octazid | ||
+ | ===================================================================== | ||
+ | * Simple dialog to create a new file in a folder | ||
+ | ********************************************************************/ | ||
+ | |||
+ | |||
+ | //*** CreateNewFile Dialog***// | ||
+ | function NewFileDialog(savepath) | ||
+ | { | ||
+ | var self = this; | ||
+ | this.Nfdlg = app.CreateDialog(" | ||
+ | this.Nflay = app.CreateLayout( " | ||
+ | this.txtpath = app.CreateText(savepath, | ||
+ | this.txtpath.SetMargins(0, | ||
+ | this.txtpath.parent = self; | ||
+ | this.Nflay.AddChild(this.txtpath); | ||
+ | this.Nfedt = app.CreateTextEdit("", | ||
+ | this.Nfedt.SetTextSize(18); | ||
+ | this.Nfedt.SetHint(" | ||
+ | this.Nfedt.parent = self; | ||
+ | this.Nflay.AddChild(this.Nfedt); | ||
+ | this.Nfbtnlay = app.CreateLayout( " | ||
+ | this.Nfdlgbtn = app.CreateButton(" | ||
+ | this.Nfdlgbtn.parent = self; | ||
+ | this.Nfdlgbtn.SetOnTouch(Nfdlgbtn_OnTouch); | ||
+ | this.Nfbtnlay.AddChild(this.Nfdlgbtn); | ||
+ | this.NfdlgbtnCancel = app.CreateButton(" | ||
+ | this.NfdlgbtnCancel.parent = self; | ||
+ | this.NfdlgbtnCancel.SetOnTouch(Nfdlgbtn_OnTouch); | ||
+ | this.Nfbtnlay.AddChild(this.NfdlgbtnCancel); | ||
+ | this.Nflay.AddChild(this.Nfbtnlay) | ||
+ | this.Nfdlg.AddLayout(this.Nflay); | ||
+ | this.Show = function(){self.Nfdlg.Show(); | ||
+ | this.Hide = function(){self.Nfdlg.Hide(); | ||
+ | }//function NewFileDialog() | ||
+ | |||
+ | |||
+ | // Called if a button is touched | ||
+ | function Nfdlgbtn_OnTouch() | ||
+ | { | ||
+ | var par = this.parent; | ||
+ | file = par.Nfedt.GetText(); | ||
+ | path = par.txtpath.GetText(); | ||
+ | if(this.GetText() | ||
+ | par.Hide(); | ||
+ | }//function dlgbtn_OnTouch | ||
+ | |||
+ | |||
+ | // Called if Button Ok is touched | ||
+ | function CreateFile(filename, | ||
+ | { | ||
+ | if (filename != "" | ||
+ | { | ||
+ | //Replace illegal letters | ||
+ | filename = filename.replace(/ | ||
+ | |||
+ | //Replace double Whitespaces | ||
+ | filename = filename.replace(/ | ||
+ | |||
+ | //Replace Whitespaces at the start and at the end | ||
+ | filename = filename.trim(); | ||
+ | |||
+ | //Check if File exists. if yes, show a message | ||
+ | if (app.FileExists(path + filename)) | ||
+ | { | ||
+ | app.Alert(" | ||
+ | "Error - Filename already exists!" | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | //Create the file | ||
+ | app.WriteFile(path + filename); | ||
+ | //show a message if creation was ok | ||
+ | if (app.FileExists(path + filename)) | ||
+ | app.ShowPopup(" | ||
+ | } | ||
+ | } | ||
+ | }//function CreateFile | ||
+ | </ | ||
+ | |||
+ | In your app you have to do just three things,\\ \\ | ||
+ | 1. Load the Script with | ||
+ | app.LoadScript(" | ||
+ | 2. Create a Button to call the Dialog and add it to the layout | ||
+ | btn = app.CreateButton( " | ||
+ | btn.SetOnTouch(CreateNewFile); | ||
+ | lay.AddChild( btn ); | ||
+ | 3. Write a function to call the Dialog | ||
+ | function CreateNewFile() | ||
+ | { | ||
+ | // write the path where you want create the file | ||
+ | DlgNewFile = new NewFileDialog("/ | ||
+ | DlgNewFile.Show(); | ||
+ | } | ||
+ | Or you can use this Example. | ||
+ | <code Javascript exampleapp.js> | ||
+ | //Called when application is started. | ||
+ | function OnStart() | ||
+ | { | ||
+ | app.LoadScript(" | ||
+ | lay = app.CreateLayout( " | ||
+ | btn = app.CreateButton( " | ||
+ | btn.SetOnTouch(CreateNewFile); | ||
+ | lay.AddChild( btn ); | ||
+ | app.AddLayout( lay ); | ||
+ | } | ||
+ | |||
+ | |||
+ | //Create a new Dialog | ||
+ | function CreateNewFile() | ||
+ | { | ||
+ | // write the path where you want create the file | ||
+ | DlgNewFile = new NewFileDialog("/ | ||
+ | DlgNewFile.Show(); | ||
+ | } | ||
+ | </ |