Передача пользовательского ввода из editText в editText в xml - PullRequest
0 голосов
/ 19 апреля 2020

Я создаю свое первое приложение и пытаюсь передать пользовательский ввод, который вводится в одно поле editText, автоматически заполняется в другое поле editText. Я уже провел какое-то исследование, но ничего не смог найти по этому поводу. Надеюсь, кто-то может дать мне подсказку.

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

После того, как этот ввод в первом поле завершен, а второе заполнено автоматически, пользователь все еще может решить, какой ввод нужно выдать, пока кнопка не будет нажата.

1 Ответ

0 голосов
/ 19 апреля 2020

В вашей деятельности.

public class MainActivity extends Activity{

private EditText editText1;
private EditText editText2;

В метод создания добавьте все ниже

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) {


    }
});
...