N
N
neuralnetwork20202021-03-30 16:26:32
Kotlin
neuralnetwork2020, 2021-03-30 16:26:32

Android Studio how to add values ​​to EditText field on button click?

Hello

I am writing a calculator program in Kotlin. First I repainted the background of the program in gray

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>



Background

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



Then I created a field for entering values ​​in the program

Field

<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"
            />



And added some buttons

+ button

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



Other buttons

<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="="
            />



Now the question itself: How can I make a special character inserted into the EditText field when clicking on the button (when clicking on "+", it was written in the "+" field. When clicking on "6", the number "6" was written in the field, etc.) d.?

Program screenshot

606325d2cb38e990428658.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dewerro, 2021-03-30
@Dewerro

I think in your case it is better to use TextView instead of EditText.
Add an id to the buttons.
Button handler: button.setOnClickListener { setTextFields("+") } // where button is the id of
your button

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question