@neuralnetwork2020
Junior Android Developer

Android Studio как добавить значения в поле EditText, при нажатии на кнопку?

Здравствуйте

Я пишу программу калькулятор на языке Kotlin. Сначала я перекрасил фон программы серым цветом

square.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/background">
    <solid android:color="#D3D3D3"/>
    </shape>



Фон

<RelativeLayout
        android:id="@+id/squarefigure"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/square"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" >



Потом я создал в программе поле для ввода значений

Поле

<EditText
            android:id="@+id/edittext"
            android:layout_width="340dp"
            android:layout_height="125dp"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="10dp"
            android:gravity="bottom"
            android:focusable="false"
            />



И добавил несколько кнопок

Кнопка +

<Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="160dp"
            android:text="+"
            android:onClick="ButtonPlus"
            />



Остальные кнопки

<Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="60dp"
            android:layout_marginTop="160dp"
            android:text="-"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="110dp"
            android:layout_marginTop="160dp"
            android:text="*"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="210dp"
            android:layout_marginTop="160dp"
            android:text="7"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="260dp"
            android:layout_marginTop="160dp"
            android:text="8"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="310dp"
            android:layout_marginTop="160dp"
            android:text="9"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="310dp"
            android:layout_marginTop="240dp"
            android:text="6"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="260dp"
            android:layout_marginTop="240dp"
            android:text="5"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="210dp"
            android:layout_marginTop="240dp"
            android:text="4"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="110dp"
            android:layout_marginTop="240dp"
            android:text="\\"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="60dp"
            android:layout_marginTop="240dp"
            android:text="%"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="240dp"
            android:text="/"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="310dp"
            android:layout_marginTop="320dp"
            android:text="3"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="260dp"
            android:layout_marginTop="320dp"
            android:text="2"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="210dp"
            android:layout_marginTop="320dp"
            android:text="1"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="110dp"
            android:layout_marginTop="320dp"
            android:text="p"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="60dp"
            android:layout_marginTop="320dp"
            android:text="√"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="320dp"
            android:text="."
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="260dp"
            android:layout_marginTop="400dp"
            android:text="0"
            />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="60dp"
            android:layout_marginTop="400dp"
            android:text="="
            />



Теперь сам вопрос: Как мне сделать чтобы при нажатии на кнопку вставлялся в поле EditText специальный символ(при нажатии на "+", писалось в поле "+". При нажатии на "6", писалось в поле цифра "6" и т.д.?

Скриншот программы

606325d2cb38e990428658.jpeg
  • Вопрос задан
  • 613 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы