简体   繁体   中英

Passing user input from editText to editText in xml

I am building my first application and trying to pass the user input which is typed in into one editText field is automatically filled in into another editText field. I already did some research but could not find anything about that. Hope someone is able to give me a hint.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content"
    android:fillViewport="true">
    <LinearLayout
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="830dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="@+id/editText"
            app:layout_constraintTop_toBottomOf="@+id/editText">
            <EditText
                android:id="@+id/editText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentBottom="true"
                android:layout_marginStart="-3dp"
                android:layout_marginEnd="-3dp"
                android:layout_marginBottom="665dp"
                android:layout_toEndOf="@+id/spinner1"
                android:autofillHints=""
                android:ems="10"
                android:gravity="start|top"
                android:hint="@string/kunden_vertrags_nr"
                android:inputType="textMultiLine"
                app:layout_constraintBottom_toBottomOf="@+id/spinner1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/spinner1"
                app:layout_constraintTop_toBottomOf="@+id/spinner1"
                app:layout_constraintVertical_bias="0.0"
                tools:layout_conversion_absoluteHeight="45dp"
                tools:layout_conversion_absoluteWidth="411dp"
                tools:targetApi="o" />
            <EditText
                android:id="@+id/editText3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginStart="8dp"
                android:layout_marginTop="211dp"
                android:autofillHints=""
                android:ems="10"
                android:hint="@string/terminwunsch"
                android:inputType="date"
                app:layout_constraintBottom_toTopOf="@+id/editText2"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.08"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView"
                app:layout_constraintVertical_bias="0.127" />
        </RelativeLayout>
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="createMyPDF"
            android:text="@string/pdferstellen"
            tools:layout_conversion_absoluteHeight="48dp"
            tools:layout_conversion_absoluteWidth="411dp" />
    </LinearLayout>
</ScrollView>

After that input in the first field is done and the second is automatically is filled in, the user still can decide what input the want to give until the button is pushed.

In your activity.

public class MainActivity extends Activity{

private EditText editText1;
private EditText editText2;

In on create method add all the below

editText1 = findViewById(R.id.editText);
editText2 = findViewById(R.id.editText3);


//lets say when typing in editText1 you want editText2 to fill automatically

editText1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

//here we set editText2 everytime we type in editText1

editText2.setText(s);
}

@Override
public void afterTextChanged(Editable s) {


    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM