Есть ли способ проверить, заполнены ли текст для редактирования электронной почты и пароля, перед тем как исключить намерение кнопки входа? - PullRequest
0 голосов
/ 25 февраля 2020

Я новичок в android. Я создал страницу входа с электронной почтой и паролем editTexts и кнопкой входа. Как я могу проверить, заполнены ли адрес электронной почты и пароль перед выполнением намерения кнопки входа? и как объявляется условное намерение кнопки входа в систему?

вот мой xml код

    <EditText
        android:id="@+id/emailEt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="@string/emailEt"
        android:inputType="textEmailAddress"

        android:paddingStart="8dp"
        android:paddingEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.025"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="parent" />

    <EditText
        android:id="@+id/passwordEt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="@string/passwordEt"
        android:inputType="textPassword"
        android:paddingStart="8dp"
        android:paddingEnd="8dp"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="@+id/parent"
        app:layout_constraintTop_toBottomOf="@+id/parent" />

    <Button
        android:id="@+id/loginBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="@string/loginBtn"
        android:textAllCaps="false"
        android:textSize="15sp"
        android:onClick="login"


        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/passwordEt" />


</androidx.constraintlayout.widget.ConstraintLayout>

Ответы [ 3 ]

2 голосов
/ 25 февраля 2020

Да, есть много способов, которыми это может быть обработано. Простой поиск в Google мог бы помочь вам.

Самый простой способ - установить OnClickListener () для кнопки входа в систему. Метод слушателя будет вызываться при нажатии кнопки.

Внутри метода вы можете проверить, является ли поле Edittext пустым, используя TextUtils.isEmpty ("TEXT INSIDE EDITEXT") метод .

Текст внутри текста редактирования можно прочитать с помощью метода getText () .

Пожалуйста, ознакомьтесь с приведенным ниже кодом для справки.

    public OnClickListener onLoginClick = new OnClickListener() 
{

    @Override
    public void onClick(DialogInterface dialog, int which)
    {
        EditText emailField = (EditText)layout.findViewById(R.id. emailEt);
        String email = emailField.getText().toString()

        if(TextUtils.isEmpty(email))
        {
           // Show error here
        }
        else
        {
          // Do the necessary action here.
        }

    }
};

Аналогичным образом добавьте проверку для поля пароля.

В файле XML установите слушателя на кнопку входа в систему следующим образом.

android:onClick="onLoginClick"
1 голос
/ 25 февраля 2020
yourEditext.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //Here you can store email into variable before clicking button
        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {

        // TODO Auto-generated method stub
    }
});
0 голосов
/ 25 февраля 2020

Да, вы можете использовать myEditText.addTextChangedListener(), когда пользователь печатает, вы получите адрес электронной почты и пароль

emailEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //Here you can store email into variable before clicking button
        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {

        // TODO Auto-generated method stub
    }
});
...