本文涵盖了书中的3.1---3.3章节的大部分代码,还有资源,供朋友们参考。虽然有点乱,但是基本上都是能运行的代码,因为在我的机器上都运行过。本文不是Kotlin语言,实现环境是在AndroidStudio中实现的,也没有步骤,只有最后的代码,供参考。
每个Activity创建后都要在这个Manifest.xml文件中注册一下,前前后后创建了三个Activity。
AndroidManifest.xml的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.NewActivityTest"
tools:targetApi="31">
<activity
android:name=".ThirdActivity"
android:exported="false"
tools:ignore="AppLinkUrlError">
<intent-filter tools:ignore="AppLinkUrlError">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="https" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true"
android:label="This is First Activity Label">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:exported="false">
<intent-filter>
<action android:name="com.example.newactivitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.newactivitytest.MY_CATEGORY" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.kt 文件内容如下:(书中是FirstActivity)主要实现的功能有:点击按钮,显示出toast,就是一小段信息文本,然后过一会就自己消失了。加载网页、显示拨号界面、向下一个Activity传递数据。
package com.example.newactivitytest
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ContextMenu
import android.view.Menu
import android.view.View
import android.widget.Button
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button1: Button = findViewById(R.id.button1)
button1.setOnClickListener{
//Toast是Android系统提供的一种非常好的提醒方式,在程序中可以使用它将一些短小的信息通
//知给用户,这些信息会在一段时间后自动消失,并且不会占用任何屏幕空间
// Toast.makeText(this, "You clicked Button 1", Toast.LENGTH_SHORT).show()
//最原始的写法,为了实现隐式的intent ,该行代码被注释掉了
// val intent = Intent(this, SecondActivity::class.java)
//为了演示加载网页的功能,下面两行代码注释掉
// val intent = Intent("com.example.newactivitytest.ACTION_START")
// intent.addCategory("com.example.newactivitytest.MY_CATEGORY")
//以下两行代码点击按钮1,会出现百度的网页
// val intent=Intent(Intent.ACTION_VIEW)
// intent.data= Uri.parse("https://www.baidu.com")
//显示拨号
// val intent=Intent(Intent.ACTION_DIAL)
// intent.data=Uri.parse("tel:10086")
//点击按钮1,会向下一个Activity传递数据
val data="Hello SecondActivity"
val intent = Intent(this, SecondActivity::class.java)
//putExtra()方法接收两个参数:参数一是 键 之后取值的时候用,参数二是需要真正传递的数据
intent.putExtra("extra_data",data)
startActivity(intent)
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main, menu)
return true
// return super.onCreateOptionsMenu(menu)
}
}
SecondActivity.kt 文件内容:(带有一个按钮,点击按钮显示Toast,以及提取数据)
package com.example.newactivitytest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.second_layout)
val button2: Button = findViewById(R.id.button2)
button2.setOnClickListener{
Toast.makeText(this, "You clicked Button 2", Toast.LENGTH_SHORT).show()
}
//下面的代码用于从MainActiviy(书中起名为FirstActivity)中提取数据. getIntExtra() -- getBooleanExtra()
val extraData=intent.getStringExtra("extra_data")
Log.d("SecondActivity","extra data is $extraData")
}
}
ThirdActivity.kt文件内容:
package com.example.newactivitytest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class ThirdActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.third_layout)
}
}
布局文件内容:这就相当于是界面的描述文件,三个Activity,三个布局文件。
activity_main.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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
second_layout.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=".SecondActivity">
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
third_layout.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=".ThirdActivity">
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 3"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
还有一个main.xml 书中是一个菜单的资源文件,内容如下:(请注意:由于我使用的并不是LinearLayout,我用的是contraintlayout,约束布局,因此该菜单在我的程序中并没有显示出来)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
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>
如果显示出来也是这样的结果:程序的右上角有三个点,点击后出来一个菜单,内有两个菜单项Add和Remove。