Android2:构建交互式应用

一。创建项目

项目名Beer Adviser

二。更新布局

activity_main.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              xmlns:tools="http://schemas.android.com/tools"
              android:gravity="center"
              android:orientation="vertical">

    <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/beer_color"
            android:layout_gravity="center"
            android:layout_margin="16dp"
            android:entries="@array/beer_colors"/>

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/find_beer"
            style="?android:attr/buttonBarButtonStyle"
            android:id="@+id/find_beer"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="24dp"
            android:id="@+id/brands"
            tools:text="@string/brands"/>

</LinearLayout>

三。增加资源
strings.xml

复制代码
<resources>
    <string name="app_name">Beer Adviser</string>
    <string name="find_beer">Find Beer</string>
    <string name="brands">No beer selected</string>
    <string-array name="beer_colors">
        <item>Light</item>
        <item>Amber</item>
        <item>Brown</item>
        <item>Dark</item>
    </string-array>
</resources>

四。响应点击

MainActivity.kt

复制代码
package com.demo.beeradviser

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Spinner
import android.widget.TextView

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

        val findBeer = findViewById<Button>(R.id.find_beer)
        findBeer.setOnClickListener {
            val beerColor = findViewById<Spinner>(R.id.beer_color)
            val color = beerColor.selectedItem
            val beerList = getBeers(color.toString())
            val beers = beerList.reduce { str, item -> str + '\n' + item }
            val brands = findViewById<TextView>(R.id.brands)
            brands.text = beers
        }
    }

    fun getBeers(color: String): List<String> {
        return when (color) {
            "Light" -> listOf("Jail Pale Ale", "Lager Lite")
            "Amber" -> listOf("Jack Amber", "Red Moose")
            "Brown" -> listOf("Brown Bear Beer", "Bock Brownie")
            else -> listOf("Gout Stout", "Dark Daniel")
        }
    }
}

知识点:

相关推荐
nono牛32 分钟前
Android init服务.rc文件,支持开机自启动和手动控制
android
消失的旧时光-19431 小时前
从前端路由到 Android ARouter:观察者模式在不同平台的同一种落地
android·前端·观察者模式·flutter
nono牛1 小时前
安卓rc-属性触发Hello World服务设计
android
2501_915918411 小时前
iOS 图片资源保护方法,分析图片在二次打包和资源篡改中的实际风险
android·ios·小程序·https·uni-app·iphone·webview
2501_937193141 小时前
中兴机顶盒纯净固件|多机型适配+刷机解析
android·源码·源代码管理·机顶盒
TAEHENGV1 小时前
提醒列表模块 Cordova 与 OpenHarmony 混合开发实战
android·java·harmonyos
渡我白衣1 小时前
计算机组成原理(10):逻辑门电路
android·人工智能·windows·嵌入式硬件·硬件工程·计组·数电
Zender Han1 小时前
Flutter EasyRefresh 最新版本:自定义 Header / Footer 详解与实践
android·flutter·ios
鹏程十八少2 小时前
Android ANR项目实战:Reason: Broadcast { act=android.intent.action.TIME_TICK}
android·前端·人工智能
TheNextByte12 小时前
如何将Android中的照片传输到Windows 11/10
android·windows