infalte(这个参数是通过那个资源文件创建菜单,创建的菜单项添加到那个Menu对象当中)
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
super.getMenuInflater()
menuInflater.inflate(R.menu.main,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
}
}
摧毁一个Activity: finish()方法
方式一:在监听器中
val button:Button=findViewById(R.id.button)
button.setOnClickListener {
finish()
}
方式二:手机back键
使用Intent在Activity中穿梭
主Activity跳转到其它Activity
显示Intent
//第一个参数就是主Activi作为上下文,第二个参数就是传入的要启动的目标Activity
val intent=Intent(this,SecondActivity::class.java)
//启动
startActivity(inten)
第一步兴建一个Activity,在主FirstActivity中修改代码,
val button:Button=findViewById(R.id.button)
button.setOnClickListener {
val intent=Intent(this,SecondActivity::class.java)
startActivity(intent)
}
隐式的Intent
不明确指定哪一个Activity,而是指定抽象的action和category,交由系统分析启动哪一个Activity
修改AndroidManifest.xml代码
//这是一个默认的category在FirstActivity中启动匹配
val button:Button=findViewById(R.id.button)
button.setOnClickListener {
val intent=Intent(“com.example.activitytest.ACTION_START”)
startActivity(intent)
}
更多隐式Intent
调用系统浏览器来启动网页
将一个网址字符串解析成一个Uri对象
在通过Intent的setdata将Uri对象传递出去
intent.data=Uri.parse(“https://www.baidu.com”)
val button:Button=findViewById(R.id.button)
button.setOnClickListener {
//这是安卓内置动作
val intent=Intent(Intent.ACTION_VIEW)
intent.data= Uri.parse(“https://www.baidu.com”)
startActivity(intent)
}
在data标签主要匹配内容
android:scheme 用于指定数据的协议部分,如www.baidu.com中的http部分。 android:host 用于指定数据的主机名部分,如www.baidu.com中的www.baidu.com部分。 android:port 用于指定数据的端口部分,一般紧随在主机名之后,如www.rowyer:8080/mypath中的808… android:path 用于指定主机名和端口之后的部分,如一段网址中跟在域名之后的内容,如www.rowyer:8080/mypath中的/my… android:mimeType 用于指定可以处理的数据类型,允许使用通配符的方式进行指定。 ———————————————— 兴建一个Activity,修改注册信息
//无视警告
//a