App-Kotlin入门到精通-启动Activity(四)

宁教我负天下人,休教天下人负我!——曹操

App-Kotlin入门到精通-启动Activity(四)_第1张图片 App-Kotlin入门到精通-启动Activity(四)_第2张图片

 

App-Kotlin入门到精通-启动Activity(四)_第3张图片

AndroidManifest.xml注册 

......


            
                

                
            


......

创建布局activity_main.xml




    

App-Kotlin入门到精通-启动Activity(四)_第4张图片

AndroidManifest.xml注册

 
            
            
        

创建activity_two.xml布局




    

创建Intent-与TwoActivity建立关联

 /** Called when the user taps the Send button */
    fun sendMessage(view: View) {
        val textView = findViewById(R.id.textView)
        val message = textView.text.toString()
        val intent = Intent(this, TwoActivity::class.java).apply {
            putExtra(EXTRA_MESSAGE, message)
        }
        startActivity(intent)
    }

修改布局activity_main.xml




    

    

 接受MainActivity传递的消息 

class TwoActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_two)

        // Get the Intent that started this activity and extract the string
        val message = intent.getStringExtra(EXTRA_MESSAGE)

        // Capture the layout's TextView and set the string as its text
        val textView = findViewById(R.id.textView).apply {
            text = message
        }
    }
}

 范型 Class

 fun    comToActivity(t: Class,message : String) {
        val intent=Intent(this, t).apply {
            putExtra(EXTRA_MESSAGE, message)
        };
        startActivity(intent)
    }
class MainActivity : AppCompatActivity() {



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    /** Called when the user taps the Send button */
    fun sendMessage(view: View) {
        val textView = findViewById(R.id.textView)
        val message = textView.text.toString()
        comToActivity(TwoActivity::class.java,message);
    }

    fun    comToActivity(t: Class,message : String) {
        val intent=Intent(this, t).apply {
            putExtra(EXTRA_MESSAGE, message)
        };
        startActivity(intent)
    }
}

正确使用 android:parentActivityName

 
            
            
        

        
            
            
        

 

 
            
            
        

        
            
            
        

取消导航栏默认返回图标


        

        
           
        
App-Kotlin入门到精通-启动Activity(四)_第5张图片     App-Kotlin入门到精通-启动Activity(四)_第6张图片

下载资源

参考:

Kotlin范型:https://www.kotlincn.net/docs/reference/generics.html

Kotlin 中的泛型介绍:https://zhuanlan.zhihu.com/p/78721880

【码上开学】Kotlin 的泛型:https://www.bilibili.com/video/av66340216/

Kotlin学习笔记:泛型:https://www.jianshu.com/p/ea7e8356a23b

 

你可能感兴趣的:(App-Kotlin)