《第一行代码——Android》学习(二)- 菜单和Intent - Kotlin版


背景

在这里记录一下学习《第一行代码——Android》一书1的心得体会。本文的目的是实现用Kotlin编码一个菜单,然后实现两个Activity之间的信息互传。

App总体设计

FirstActivity包括4个按钮和一个菜单,功能如下:

  • 按键button1,按下之后广播一条“已经按下本按钮”提示信息。
  • 按钮btn_hello,按下之后向SecondActivity发送一个字符串信息。
  • 按钮btn_forResult,按下之后跳转到SecondActivity,并在Intent中附加RequestCode,如果SecondActivity返回一个字符串信息,则在屏幕上广播这条信息。
  • 按钮btn_quit,按下之后销毁当前Activity。
  • 菜单包括两个选项:Add和Remove,点击任意一个都会广播一条“已经按下本菜单项目”提示信息。

SecondActivity包括5个按钮,功能如下:

  • 按钮btn_to_baidu,按下之后会跳转到百度网站首页。
  • 按钮btn_to_tel,按下之后会跳转到拨打10086的拨号页面
  • 按钮btn_receive,按下之后接收FirstActivity中按钮btn_hello发来的字符串消息,并以广播形式在屏幕中显示。
  • 按钮btn_sendBack,与FirstActivity中按钮btn_forResult相对应,向FirstActivity发送一个字符串消息,并在Intent中附加ResultCode,随即跳转到FirstActivity。
  • 按钮btn_quit,按下之后销毁当前Activity(主要想测试一下两个Activity有同名控件的情况2)。

FirstActivity菜单xml文件的代码

res/menu/main.xml的完整代码如下:

<?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>

FirstActivity布局文件的代码

first_layout的完整代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1" />
    <Button
        android:id="@+id/btn_hello"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SEND HELLO" />
    <Button
        android:id="@+id/btn_forResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SEND AND FOR RESULT" />
    <Button
        android:id="@+id/btn_quit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="QUIT" />
</LinearLayout>

FirstActivity代码文件的代码

FirstActivity.kt的完整代码如下:

package com.example.firstandroid_noactivity
import androidx.appcompat.app.AppCompatActivity
import android.widget.*
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.content.Intent
class FirstActivity : AppCompatActivity() {
    
    
    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.first_layout)
        val button1: Button = findViewById(R.id.button1)
        button1.setOnClickListener {
    
    
            Toast.makeText(this, "You clicked Button 1", Toast.LENGTH_SHORT).show()
        }
        val btn_hello: Button = findViewById(R.id.btn_hello)
        btn_hello.setOnClickListener {
    
    
            Toast.makeText(this, "You clicked SEND HELLO", Toast.LENGTH_SHORT).show()
            val intent = Intent(this, SecondActivity::class.java)
            intent.putExtra("extra_data", "Hello SecondActivity")
            startActivity(intent)
        }
        val btn_forResult: Button = findViewById(R.id.btn_forResult)
        btn_forResult.setOnClickListener {
    
    
            val intent = Intent(this, SecondActivity::class.java)
            startActivityForResult(intent, 1)
        }
        val btn_quit: Button = findViewById(R.id.btn_quit)
        btn_quit.setOnClickListener {
    
    
            finish()
        }
    }
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    
    
        menuInflater.inflate(R.menu.main, menu)
        return true
    }
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
    
    
        when (item.itemId) {
    
    
            R.id.add_item -> Toast.makeText(this, "You clicked Add", Toast.LENGTH_SHORT).show()
            R.id.remove_item -> Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show()
        }
        return true
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    
    
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
    
    
            1 -> if (resultCode == RESULT_OK) {
    
    
                var returnedData = data?.getStringExtra("data_return")
                Toast.makeText(this, "returned data is $returnedData", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

SecondActivity布局文件的代码

second_layout的完整代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn_to_baidu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TO BAIDU" />
    <Button
        android:id="@+id/btn_to_tel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TO TEL 10086" />
    <Button
        android:id="@+id/btn_receive"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RECEIVE" />
    <Button
        android:id="@+id/btn_sendBack"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SEND BACK" />
    <Button
        android:id="@+id/btn_quit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="QUIT" />
</LinearLayout>

SecondActivity代码文件的代码

SecondActivity.kt的完整代码如下:

package com.example.firstandroid_noactivity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import android.net.Uri
class SecondActivity : AppCompatActivity() {
    
    
    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.second_layout)
        val btn_to_baidu: Button = findViewById(R.id.btn_to_baidu)
        btn_to_baidu.setOnClickListener {
    
    
            Toast.makeText(this, "You clicked btn_to_baidu", Toast.LENGTH_SHORT).show()
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse("https://www.baidu.com")
            startActivity(intent)
        }
        val btn_to_tel: Button = findViewById(R.id.btn_to_tel)
        btn_to_tel.setOnClickListener {
    
    
            val intent = Intent(Intent.ACTION_DIAL)
            intent.data = Uri.parse("tel:10086")
            startActivity(intent)
        }
        val btn_receive: Button = findViewById(R.id.btn_receive)
        btn_receive.setOnClickListener {
    
    
            var extraData = intent.getStringExtra("extra_data")
            Toast.makeText(this, "You received $extraData", Toast.LENGTH_SHORT).show()
        }
        val btn_sendBack: Button = findViewById(R.id.btn_sendBack)
        btn_sendBack.setOnClickListener {
    
    
            var intent = Intent()
            intent.putExtra("data_return", "Hello FirstActivity")
            setResult(RESULT_OK, intent)
            finish()
        }
        val btn_quit: Button = findViewById(R.id.btn_quit)
        btn_quit.setOnClickListener {
    
    
            finish()
        }
    }
}

运行结果

按之前探索的方法 3生成apk文件,然后传输到手机上运行,结果如下:
在这里插入图片描述


  1. 郭霖 著. 第一行代码Android. 中国工信出版集团,人民邮电出版社. 2020.4 ↩︎

  2. 不同Activity中的控件可以取一样的名字吗?_百度知道 ↩︎

  3. 《Android Studio开发实战》学习(一)- Hello World_下唐人的博客-CSDN博客_android studio学习程序开发 ↩︎

猜你喜欢

转载自blog.csdn.net/quanet_033/article/details/128302173