【发布时间】:2020-08-23 05:43:31
【问题描述】:
我正在尝试编写我的第一个 android 应用程序。我过去唯一做过的编程是 4 年前的一些 html(在 cms 出现之前)
它本质上是一个检查列表,但我希望能够有多个列表,由用户提供的列表名称分隔。我有一个名为“岛屿名称”的输入文本框,但我不知道如何从用户那里捕获该文本并将其保存以备将来使用...
<?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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="189dp"
android:text="temp call islander page"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="125dp"
android:layout_marginBottom="271dp"
android:onClick="loadHome"
android:text="home"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="41dp"
android:ems="10"
android:hint="island name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="7dp"
android:layout_marginBottom="8dp"
android:text="north"
app:layout_constraintBottom_toTopOf="@+id/checkBox2"
app:layout_constraintEnd_toEndOf="@+id/checkBox2" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginBottom="88dp"
android:text="south"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/editText" />
println("your island name is $name")
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="19dp"
android:text="you entered $name"
app:layout_constraintStart_toEndOf="@+id/checkBox2"
app:layout_constraintTop_toBottomOf="@+id/checkBox2" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
-
您不能在 XML 中间编写 Kotlin 代码。程序逻辑不是由布局文件定义的。此 XML 只是描述特定视图布局的文本文档。您的 Kotlin 或 Java 代码用于定义 Activity 和 Fragments,它们在运行时读取 XML 文件以创建(“膨胀”)视图层次结构。您需要先阅读其中一个介绍性教程,然后我们才能真正以实用的方式帮助您解决任何问题。 developer.android.com/training/basics/firstapp