1. 程式人生 > >AI開發實戰3-定製自己的Screen

AI開發實戰3-定製自己的Screen

3 Screen的定製

    Screen可以說是App Inventor2開發的最基礎元件了,其對應的原始碼在/appinventor/components/src/com/google/appinventor/components/runtime/Form.java

使用App Inventor2開發的APP,都有兩個功能選單:Stopthis application和About thisapplication,這兩個選單使用的字串都是英文,為方便使用,修改為中文,同時增加了一個更新版本的功能選單。

在Form.java中的修改如下:

//建立功能選單

@Override

 public boolean onCreateOptionsMenu(Menu menu) {

   // This procedure is called only once. To change the items dynamically

   // we would use onPrepareOptionsMenu.

   super.onCreateOptionsMenu(menu);

   // add the menu items

   // Comment out the next line if we don't want the exit button

addAboutInfoToMenu(menu);

//新增更新版本的功能選單

addUpdateToMenu(menu);

   addExitButtonToMenu(menu);

   for (OnCreateOptionsMenuListener onCreateOptionsMenuListener :onCreateOptionsMenuListeners) {

     onCreateOptionsMenuListener.onCreateOptionsMenu(menu);

    }

   return true;

  }

  //關於功能選單

 public void addAboutInfoToMenu(Menu menu) {

    //1表示此選單在選單列表中的排在第1個,就是索引值為1

   MenuItem aboutAppItem = menu.add(Menu.NONE, Menu.NONE, 1, //2,

   //"About this application")

   "關於")

   .setOnMenuItemClickListener(new OnMenuItemClickListener() {

     public boolean onMenuItemClick(MenuItem item) {

       //點選選單呼叫的函式

       showAboutApplicationNotification();

       return true;

     }

});

//功能選單icon圖示

   aboutAppItem.setIcon(android.R.drawable.sym_def_app_icon);

  }

  //更新版本功能選單

 public void addUpdateToMenu(Menu menu) {

   MenuItem updateAppItem = menu.add(Menu.NONE, Menu.NONE, 2,

   //"About this application")

   "更新版本")

   .setOnMenuItemClickListener(new OnMenuItemClickListener() {

     public boolean onMenuItemClick(MenuItem item) {

       showVersionInfo();

       return true;

     }

   });

   updateAppItem.setIcon(android.R.drawable.ic_dialog_info);

  }

//退出應用的功能選單

 public void addExitButtonToMenu(Menu menu) {

   MenuItem stopApplicationItem = menu.add(Menu.NONE, Menu.NONE, 3,//Menu.FIRST,

   //"Stop this application")

   "退出")

   .setOnMenuItemClickListener(new OnMenuItemClickListener() {

     public boolean onMenuItemClick(MenuItem item) {

       showExitApplicationNotification();

       return true;

     }

   });

   stopApplicationItem.setIcon(android.R.drawable.ic_notification_clear_all);

  }

private voidshowExitApplicationNotification() {

   //String title = "Stop application?";

   //String message = "Stop this application and exit? You'll need torelaunch " +

   //    "the application to useit again.";

   //String positiveButton = "Stop and exit";

   //String negativeButton = "Don't stop";

   String title = "提示";

   String message = "確定要退出應用?";

   String positiveButton = "確定";

   String negativeButton = "取消";

   // These runnables are passed to twoButtonAlert.  They perform the corresponding actions

   // when the button is pressed.  Here there's nothing to do for "don't stop" and cancel

   Runnable stopApplication = new Runnable() {public void run (){closeApplicationFromMenu();}};

   Runnable doNothing = new Runnable () {public void run() {}};

   Notifier.twoButtonDialog(

       this,

       message,

       title,

       positiveButton,

       negativeButton,

       false, // cancelable is false

       stopApplication,

       doNothing,

       doNothing);

  }

 private String yandexTranslateTagline = "";

 void setYandexTranslateTagline(){

   yandexTranslateTagline = "<p><small>Languagetranslation powered by Yandex.Translate</small></p>";

  }

 private void showAboutApplicationNotification() {

   //String title = "About this app";

   String title = "關於";

   String MITtagline = "<p><small><em>Invented withMIT AppInventor<br>appinventor.mit.edu</em></small></p>";

   // Users can hide the taglines by including an HTML open comment <!--in the about screen message

   //String message = aboutScreen + MITtagline + yandexTranslateTagline;

   String message = aboutScreen;

   message = message.replaceAll("\\n", "<br>");// Allow for line breaks in the string.

   //String buttonText ="Got it";

   String buttonText ="確定";

   Notifier.oneButtonAlert(this, message, title, buttonText);

  }

 private void showVersionInfo() {

Intent intent =new Intent(Intent.ACTION_VIEW);

//這裡的網址就是提供新版本供使用者下載的網址,呼叫裝置上安裝的瀏覽器開啟此網頁

   intent.setData(Uri.parse("http://xxx.xxx.xxx"));

   this.startActivity(intent);

  }

最終的實現效果如下:

在應用的底部會顯示3箇中文功能選單。

點選退出功能選單,顯示如下中文提示框: