AI开发实战3-定制自己的Screen

Posted xjbclz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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个中文功能菜单。

 

点击退出功能菜单,显示如下中文提示框:


以上是关于AI开发实战3-定制自己的Screen的主要内容,如果未能解决你的问题,请参考以下文章

AI开发实战2-定制自己的AI伴侣

AI应用开发实战 - 定制化视觉服务的使用

AI开发实战6-图像组件(Image)的定制

AI开发实战10-文件管理器(File)的定制

AI开发实战7-列表显示框(Listview)的定制

AI开发实战5-文本输入框(TextBox)的定制2