viewBinding
SpxLearnActivity.kt
package com.example.s2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.s2.databinding.L1LayoutBinding
class SpxLearnActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding=L1LayoutBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.bt1.setOnClickListener{
Toast.makeText(this,"你好,世界",Toast.LENGTH_SHORT).show()
}
}
}
l1_layout.xml
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-parcelize'
}
android {
compileSdk 30
defaultConfig {
applicationId "com.example.s2"
minSdk 21
targetSdk 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
viewBinding {
enabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
activity 调用另一个activity AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.s2">
<application
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/Theme.S2">
<activity
android:name=".AddActivity">
<intent-filter>
<action android:name="com.example.s2.ACTION_ADDS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SpxLearnActivity"
android:label="my first activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
SpxLearnActivity.kt
package com.example.s2
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import com.example.s2.databinding.L1LayoutBinding
class SpxLearnActivity : AppCompatActivity() {
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.l1_menu,menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId){
R.id.add_item->Toast.makeText(this,"增加完毕",Toast.LENGTH_SHORT).show()
R.id.remove_item->Toast.makeText(this,"减少完毕",Toast.LENGTH_SHORT).show()
}
return true
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding=L1LayoutBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.bt1.setOnClickListener{
Toast.makeText(this,"你好,世界",Toast.LENGTH_SHORT).show()
val intent= Intent("com.example.s2.ACTION_ADDS")
startActivity(intent)
}
}
}
AddActivity.kt
package com.example.s2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.s2.databinding.ActivityAddBinding
class AddActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add)
val binding= ActivityAddBinding.inflate(layoutInflater)
binding.bt1.setOnClickListener{
this.finish()
}
}
}
activity_add.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddActivity">
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="return"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="49dp"
tools:layout_editor_absoluteY="77dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
自定义category
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.s2">
<application
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/Theme.S2">
<activity
android:name=".AddActivity">
<intent-filter>
<action android:name="com.example.s2.ACTION_ADDS" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="com.example.s2.ADD_CATEGORY"/>
</intent-filter>
</activity>
<activity
android:name=".SpxLearnActivity"
android:label="my first activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.example.s2
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import com.example.s2.databinding.L1LayoutBinding
class SpxLearnActivity : AppCompatActivity() {
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.l1_menu,menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId){
R.id.add_item->Toast.makeText(this,"增加完毕",Toast.LENGTH_SHORT).show()
R.id.remove_item->Toast.makeText(this,"减少完毕",Toast.LENGTH_SHORT).show()
}
return true
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding=L1LayoutBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.bt1.setOnClickListener{
Toast.makeText(this,"你好,世界",Toast.LENGTH_SHORT).show()
val intent= Intent("com.example.s2.ACTION_ADDS")
intent.addCategory("com.example.s2.ADD_CATEGORY")
startActivity(intent)
}
}
}
|