In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about the use of Android Activity in Kotlin. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
The use of Toast in Activity Toast.makeText (this, "ADD", Toast.LENGTH_SHORT). Show () / / Toast.makeText (Activity, reminder character, length_long | short display is often long | short) Activity does not use findViewById () to get control ID
Open Gradle Scripts and add the following paragraph to the dependecies of build Gradle (Project:AppName):
Classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
Then add the following paragraph at the end of the 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 ().
Use menu Menu in bt1.setOnClickListener {...} Activity
Now under the res directory, right-click under Directory to create a new Menu directory, then create a new MainMenu file, and edit the following code:
Return MainActivity to rewrite the onCreateOptionsMenu () and onOptionsItemSelected () methods by Ctrl+O (Control+O in Mac OS)
Override fun onCreateOptionsMenu (menu: Menu?): Boolean {menuInflater.inflate (R.menu.mainframe menu) / / get the resource file return true} override fun onOptionsItemSelected (item: MenuItem): Boolean {when (item.itemId) {R.id.Addmemoritem.Toast.makeText (this, "ADD", Toast.LENGTH_SHORT). Show () R.id.RemoveSecretitemtem.Toast.makeText (this) The use of intent in "REMONE", Toast.LENGTH_SHORT) .show ()} return true} Activity
Here, create a new IntentActivity and register it in the manifest file manifest. It will generally register automatically.
.. intent explicitly / / first gets the Intent object, method (current activity, jump activity) val intent = Intent (this, IntentMainActivity::class.java) / / starts to jump startActivity (intent) intent implicitly
Intent implicitly needs to use the action and category tags in the manifest file manifest. You can add them yourself. The name of action is optional. The name of the first category needs to be set to default, and the name of the second category is optional.
Return to MainActivity and write the following code:
Val intent = Intent ("com.example.ACTION_START") intent.addCategory ("com.example.activity.CATEGORY") startActivity (intent) intent data transfer data / / MainActivityval intent = Intent (this, IntentMainActivity::class.java) / / transfer data to the IntentActivity, putExtra (name Value) intent.putExtra ("test_data", "explicit intent") startActivity (intent) / / IntentActivity / / accept the data transferred by the last activity getStringExtra (name) val data = intent.getStringExtra ("test_data") returned data / / MainActivity modify startactivity () to startActivityForResult () val intent = Intent (this, IntentMainActivity::class.java) startActivityForResult (intent,1) / / because startActivityForResult () method is used / / the onActivityResult () method of MainActivity will be called back after IntentActivity is destroyed, / / so we need to rewrite onActivityResult to get the returned data override fun onActivityResult (requestCode: Int, resultCode: Int, data: Intent?) {super.onActivityResult (requestCode, resultCode) Data) when (requestCode) {1-> if (resultCode== RESULT_OK) {val returnedData= data?.getStringExtra ("data_return") Log.d ("MainActivity", "returned data is $returnedData")}} / / IntentActivity create an intent object to pass data val intent = Intent () intent.putExtra ("data_return", "hello") MainActivity ") setResult (RESULT_OK,intent) finish () extension
Intent can jump not only to the Activity you created, but also to the system application.
For example, the browser:
Bt3.setOnClickListener {val intent = Intent (Intent.ACTION_VIEW) intent.data = Uri.parse ("https://www.baidu.com") startActivity (intent))
If you jump to the phone:
Bt4.setOnClickListener {val intent = Intent (Intent.ACTION_DIAL) intent.data = Uri.parse ("tel:10010") startActivity (intent) after reading the above, do you have any further understanding of the Activity usage of Android in Kotlin? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.