Kotlin Android Explanation of Activity

  • 2021-12-19 06:40:31
  • OfStack

The use of Toast in the directory Activity does not use findViewById () in Activity to obtain the use of intent in the menu MenuActivity in the control IDActivity. The use of intent explicit intent implicit intent data transfer transfer data return data extension reference

Use of Toast in Activity


Toast.makeText(this,"ADD",Toast.LENGTH_SHORT).show()
// Toast.makeText(Activity, Reminder characters, length_long|short  The display is often long | Short )

findViewById () is not used in Activity to get the control ID

Open Gradle Scripts Add the following paragraph to dependecies of build Gradle (Project: AppName):


 classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"

Then add the following paragraph at the end of plugins tag of build gradle (Module: AppName: app):


plugins {
   ...
}
apply plugin: 'kotlin-android-extensions'

So you can call the method directly using the control ID instead of getting it through findViewById ()


 bt1.setOnClickListener{...}

Using menu Menu in Activity

Now right-click under res to create a new Menu directory, then create a new MainMenu file, and edit the following code:


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/Add_item"
        android:title="ADD"/>
    <item
        android:id="@+id/Remove_item"
        android:title="Remove"/>
</menu>

Returning MainActivity overrides the onCreateOptionsMenu () and onOptionsItemSelected () methods as Ctrl+O (Control+O in Mac OS)


   override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main,menu) // Get the resource file written above 
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
               R.id.Add_item->Toast.makeText(this,"ADD",Toast.LENGTH_SHORT).show()
               R.id.Remove_item->Toast.makeText(this,"REMONE",Toast.LENGTH_SHORT).show()
        }
        return true
    }

Use of intent in Activity

Here first create a new IntentActivity, and in the manifest file manifest registration, 1 will automatically register


 <activity android:name=".IntentMainActivity"> ...</activity>

intent explicit


 // Get first Intent Object, method (current activity, jump activity)  
 val intent = Intent(this, IntentMainActivity::class.java)
 // Start jumping 
 startActivity(intent)

intent Implicit

intent implicitly needs to use two tags, action and category in the manifest file manifest, which you can add by yourself. name of action is optional, name of the first category needs to be set to default, and name of the second category is optional


<activity android:name=".IntentMainActivity">
            <intent-filter>
                <action android:name="com.example.ACTION_START"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="com.example.activity.CATEGORY"/>
            </intent-filter>
</activity>

Returning to MainActivity, write the following code:


 val intent = Intent("com.example.ACTION_START")
 intent.addCategory("com.example.activity.CATEGORY")
 startActivity(intent)

Transfer of intent data

Transfer data

 classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
0

Return data

 classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
1

Expand

intent can not only jump to the Activity you created, but also jump to the system application

For example, browsers:


 classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
2

If you jump to a phone:


 bt4.setOnClickListener {
            val intent = Intent(Intent.ACTION_DIAL)
            intent.data = Uri.parse("tel:10010")
            startActivity(intent)

Reference

[1] Guo Lin. Line 1 code Android [M]. 3rd Edition. Beijing. People's Post and Telecommunications Publishing House. 2020.


Related articles: