4. OnKeyUp。 使用键盘仿真从扫描仪获取条形码在TSD上的应用以及通过HTTP服务与1C:Enterprise 8.3的通信。 第5部分(菜单,伴随对象)
每个应用程序都有全局变量。 例如,其中之一是数据库的名称。 从科特林的观点来看,我不知道这是正确的。 为了设置应用程序的全局变量,我们将执行以下操作。
创建一个新的项目
Navigation Drawer Activity
。 在其中,我们将学习如何使用菜单,并使用设置创建页面。 在MainActivity旁边,创建一个新类
class MyApp: Application() { companion object { var baseUrl = "http://192.168.0.1/unf/hs/inntsd/v1/" const val dbName = "mws_db" var user1C = "tsd" var pass1C = "123123" } }
在
<application
标签的
AndroidManifest.xml
中,添加以下行
android:name=".MyApp"
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.domain.myapp"> <application android:name=".MyApp" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
仅此而已。 现在,从应用程序的任何部分,我们都可以通过将变量称为
MyApp.
来获取变量
MyApp.
我们转到第二部分。 让我们玩一下菜单。 让我们立即决定对每个片段都有自己的菜单。 现在在项目中看起来像这样。
创建一个新菜单。
New - Menu resource file
在
New - Menu resource file
放置以下文本。
orderInCategory
指示菜单的位置。 少即是高。 我们记得
BASIC
和数字10、20、30-因此我们将始终有机会在元素之间添加另一个项目。 -!>
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/gallery_clear" android:orderInCategory="10" android:title="" app:showAsAction="never" /> <item android:id="@+id/gallery_send" android:orderInCategory="20" android:title="" app:showAsAction="never" /> </menu>
我们认为我们的主要活动没有自己的菜单。 因此,我们大胆删除该功能
override fun onCreateOptionsMenu(menu: Menu): Boolean {
在
ui/gallery/GalleryFragment
文件中,
ui/gallery/GalleryFragment
重新定义了两个函数。
override fun onCreate(savedInstanceState: Bundle?) { setHasOptionsMenu(true)
最后我们添加功能
override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.getItemId()) { R.id.gallery_clear -> Log.d("MenuDebug", " ") R.id.gallery_send -> Log.d("MenuDebug", " ") } return super.onOptionsItemSelected(item) }
仅此而已。 通过类比每个片段,我们现在可以添加自己的菜单。