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")
        }
    }
}

知识点:

相关推荐
明雨-开发2 小时前
Android打包时候Execution failed for task ‘:launcher:lintVitalAnalyzeRelease‘.
android
游戏开发爱好者82 小时前
开心上架是什么,一站式 Apple 开发者工作台总览
android·小程序·https·uni-app·iphone·webview
Android-Flutter2 小时前
android kotlin launch 源码分析
android·kotlin
Android-Flutter3 小时前
android kotlin 状态机 Continuation 详解
android·kotlin
Zha0Zhun4 小时前
Jetpack Compose 实现 iOS 风格 3D WheelPicker
android
深念Y5 小时前
RIO-UL00(EMUI 4.1 / Android 6.0.1 / arm64)开机自启动 sshd
android·linux·华为·安卓·chroot·sshd·emui
爱和冰阔落5 小时前
【MySQL 慢查询排查实战】列表接口逐渐变慢时,怎样从请求链路定位原因
android·数据库·mysql
hunterandroid1 天前
Android WebView JSBridge 治理实战:从线上白屏崩溃到协议化通信
android·前端
一个用户名i1 天前
【Compose 系列】第 1 篇:认识 Compose,为什么要学它
android·android jetpack