как сохранить / использовать вводимые пользователем данные в android kotlin - PullRequest
1 голос
/ 07 мая 2020

Я пытаюсь написать свое самое первое android приложение. единственное программирование, которое я делал в прошлом, было около html 4 много лет go (до того, как 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>

1 Ответ

0 голосов
/ 07 мая 2020

Проще говоря, в Android dev (Android Studio) у вас есть ваше представление, которое является вашим файлом. xml, и ваша программа logi c записана в вашем .kt или. java файл, то вы ссылаетесь на соответствующее представление из файла .kt oe, java.

например,

activity_main. xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/textView"
        android:layout_height="wrap_content"
        android:layout_width="200dp"/>
    <Button
        android:id="@+id/saveBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>

MainActivity.kt:

    import kotlinx.android.synthetic.main.activity_main.* //this will reference all views in activity_main - 
//Android studio usually auto imports this when you simply type control name

    class MainActivity : AppCompatActivity() {

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

            saveBtn.setOnClickListener { 
                saveDetails()
            }
        }

        fun saveDetails(){
            var userText = textView.text.toString()
            textView4.setText(userText)
            //do something with text
        } 
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...