Activity的创建
项目中的任何Activity都应重写onCreate()方法
class FirstActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) }
默认的实现调用父类onCreate()即可
所有Activity都要在AndroidMainfest.xml中注册
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <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/AppTheme"> <activity android:name=".FirstActivity"></activity> </application> </manifest>
在application标签内通过</activity>注册,其中 .FirstActivity 省略了包名
配置主Activity
加入<intent-filter>标签
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <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/AppTheme"> <activity android:name=".FirstActivity"> android:label:="First Activity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
可以用label属性设置标题栏内容
在Activity中使用Toast
class FirstActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.first_layout) button1.setOnClickListener { Toast.makeText(this, "you are ugly", Toast.LENGTH_SHORT).show() } } }
this为上下文,传入当前Activity。Toast.LENGTH_SHORT为显示时长。获得Toast对象调用show()方法显示出来
在Activity中使用Menu
res目录下新建menu文件夹,创建menu resource file
<?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>
使用<item 添加菜单项,使用title属性指定名称
为了使用menu,在Activity中重写onCreateOptionsMenu方法
override fun onCreateOptionsMenu(menu: Menu?): Boolean { menuInflater.inflate(R.menu.main, menu) return true; }
使用R.menu.main资源文件添加到menu对象
为了让菜单响应事件,重写onOptionsItemSelected
override fun onOptionsItemSelected(item: MenuItem): Boolean { when(item.itemId){ R.id.add_item -> Toast.makeText(this, "sb", Toast.LENGTH_SHORT).show() R.id.remove_item -> Toast.makeText(this,"hah",Toast.LENGTH_SHORT).show() } return super.onOptionsItemSelected(item) }
使用finish()销毁Activity
布局文件的加载
在res/layout目录下创建Activity的布局文件,布局编写完成后须在Activity的onCreate中加载
class FirstActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.first_layout) }
setContentView(R.layout.first_layout)加载布局文件
使用intent切换Activity
1.显式intent
button1.setOnClickListener { Toast.makeText(this, "you are ugly", Toast.LENGTH_SHORT).show() val intent = Intent(this, SecondActivity::class.java) startActivity(intent) }
2.隐式intent
指定action和category,交由系统找出合适的activity启动
button1.setOnClickListener { val intent = Intent(Intent.ACTION_VIEW) intent.data = Uri.parse("https://www.baidu.com") startActivity(intent) }
向下一个activity传递数据
通过putExtra将数据暂存intent中
button1.setOnClickListener { val data = "hahaha" val intent = Intent(this, SecondActivity::class.java) intent.putExtra("data", data) startActivity(intent) }
接收
class SecondActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_second) val data = intent.getStringExtra("data") Toast.makeText(this, "data is $data", Toast.LENGTH_SHORT).show() } }
返回数据给上一个Activity
使用startActivityForResult
button1.setOnClickListener { val intent = Intent(this, SecondActivity::class.java) startActivityForResult(intent,1) }
button2.setOnClickListener { val intent = Intent() intent.putExtra("data_return","haahahah") setResult(Activity.RESULT_OK,intent) finish() }
override fun onActivityResult(requestCode:Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when(requestCode){ 1 -> if(resultCode == Activity.RESULT_OK){ val returned_data = data?.getStringExtra("data_return") Toast.makeText(this, "$returned_data", Toast.LENGTH_SHORT).show() } } }
原文:https://www.cnblogs.com/hzoi-poozhai/p/12682497.html